Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Implementing #define macros similar to C on python

67 views
Skip to first unread message

JL

unread,
Nov 14, 2013, 9:29:48 PM11/14/13
to
One of my favorite tools in C/C++ language is the preprocessor macros.

One example is switching certain print messages for debugging use only

#ifdef DEBUG_ENABLE
DEBUG_PRINT print
#else
DEBUG_PRINT

Is it possible to implement something similar in python? Thank you.

Dave Angel

unread,
Nov 14, 2013, 10:30:51 PM11/14/13
to pytho...@python.org
On Thu, 14 Nov 2013 18:29:48 -0800 (PST), JL <light...@gmail.com>
wrote:
Sure. A preprocessor can be written for nearly every language. Are
you offering?

--
DaveA

Chris Angelico

unread,
Nov 14, 2013, 10:49:52 PM11/14/13
to pytho...@python.org
There are usually other ways to do things. For instance, you can
define a function to either do something or do nothing:

if debug_mode:
debug_print = print
else:
debug_print = lambda: None

debug_print("This won't be shown unless we're in debug mode!")

But as Dave says, you could write a preprocessor if you need one.

ChrisA

Roy Smith

unread,
Nov 14, 2013, 11:10:03 PM11/14/13
to
In article <fae7479b-ecec-4114...@googlegroups.com>,
Why would you want to? One of the most horrible things about C/C++ is
the preprocessor. Python has much better mechanisms to implement just
about anything you would do with the preprocessor.

For the example you gave, you would log things as info() or debug(), and
then adjust the filter level in the logger.

Chris Angelico

unread,
Nov 14, 2013, 11:57:05 PM11/14/13
to pytho...@python.org
On Fri, Nov 15, 2013 at 3:10 PM, Roy Smith <r...@panix.com> wrote:
> Why would you want to? One of the most horrible things about C/C++ is
> the preprocessor.

Hey, that's not fair! Without the preprocessor, how would you be able
to do this:

//Hide this part away in a header file somewhere
struct b0rkb0rk
{
float value;
b0rkb0rk(float v):value(v) {}
operator float() {return value;}
float operator +(float other) {return value+other-0.1;}
};
//Behold the power of the preprocessor!
#define float b0rkb0rk

//Okay, now here's your application
#include <iostream>

int main()
{
std::cout << "Look how stupidly inaccurate float is!\n";
float x = 123.0f;
std::cout << "123.0 + 2.0 = " << x + 2.0f << "\n";
std::cout << "See? You should totally use double instead.\n";
}

(Anybody got a cheek de-tonguer handy? I think it's stuck.)

ChrisA

Irmen de Jong

unread,
Nov 15, 2013, 6:20:49 PM11/15/13
to
You could just run cpp (or gcc -E) on your python-with-macros-file to generate the final
.py file. But: yuck, eww, gross.

Irmen


JL

unread,
Nov 15, 2013, 6:36:43 PM11/15/13
to
Thanks! This is the answer which I am seeking. However, I am not able to get the following line to work. I am using python 2.7.5

debug_print = print

Can we assign a function into a variable in this manner?

Irmen de Jong

unread,
Nov 15, 2013, 7:20:25 PM11/15/13
to
On 16-11-2013 0:36, JL wrote:
> Thanks! This is the answer which I am seeking. However, I am not able to get the following line to work. I am using python 2.7.5
>
> debug_print = print
>
> Can we assign a function into a variable in this manner?

Yes, functions are just another object. But 'print' is only a function as of Python 3.
For your version, try adding this as the first line:
from __future__ import print_function

Irmen

Terry Reedy

unread,
Nov 15, 2013, 7:22:06 PM11/15/13
to pytho...@python.org
On 11/15/2013 6:36 PM, JL wrote:
> Thanks! This is the answer which I am seeking. However, I am not able to get the following line to work. I am using python 2.7.5
>
> debug_print = print

Start your file with
from __future__ import print_function
and the above should work.

Oh, and please snip stuff not relevant to your post.

--
Terry Jan Reedy

Mark Lawrence

unread,
Nov 15, 2013, 7:22:25 PM11/15/13
to pytho...@python.org
Yes but please don't top post. Actually print is a statement in Python
2 so your code should work if you use

from __future__ import print_function

at the top of your code.

Would you also be kind enough to read and action this
https://wiki.python.org/moin/GoogleGroupsPython to prevent the double
line spacing shown above, thanks.

--
Python is the second best programming language in the world.
But the best has yet to be invented. Christian Tismer

Mark Lawrence

JL

unread,
Nov 16, 2013, 12:38:44 AM11/16/13
to
On Saturday, November 16, 2013 8:22:25 AM UTC+8, Mark Lawrence wrote:

> Yes but please don't top post. Actually print is a statement in Python
> 2 so your code should work if you use
> from __future__ import print_function
> at the top of your code.
> Would you also be kind enough to read and action this
> https://wiki.python.org/moin/GoogleGroupsPython to prevent the double
> line spacing shown above, thanks.

Thank you for the tip. Will try that out. Hope I get the posting etiquette right this time.

Serhiy Storchaka

unread,
Nov 16, 2013, 5:02:43 AM11/16/13
to pytho...@python.org
15.11.13 06:57, Chris Angelico написав(ла):
> On Fri, Nov 15, 2013 at 3:10 PM, Roy Smith <r...@panix.com> wrote:
>> Why would you want to? One of the most horrible things about C/C++ is
>> the preprocessor.
>
> Hey, that's not fair! Without the preprocessor, how would you be able
> to do this:
>
> //Hide this part away in a header file somewhere
> struct b0rkb0rk
> {
> float value;
> b0rkb0rk(float v):value(v) {}
> operator float() {return value;}
> float operator +(float other) {return value+other-0.1;}
> };
> //Behold the power of the preprocessor!
> #define float b0rkb0rk
>
> //Okay, now here's your application
> #include <iostream>
>
> int main()
> {
> std::cout << "Look how stupidly inaccurate float is!\n";
> float x = 123.0f;
> std::cout << "123.0 + 2.0 = " << x + 2.0f << "\n";
> std::cout << "See? You should totally use double instead.\n";
> }
>
> (Anybody got a cheek de-tonguer handy? I think it's stuck.)

>>> class b0rkb0rk(float):
... def __add__(self, other):
... return super().__add__(other) - 0.1
...
>>> import builtins
>>> builtins.float = b0rkb0rk
>>> float(123) + 2
124.9


Mark Lawrence

unread,
Nov 16, 2013, 6:48:45 AM11/16/13
to pytho...@python.org
No problem. It's not a matter of etiquette, it's using a tool that's
not flawed :)
0 new messages