Implementing #define macros similar to C on python

J

JL

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.
 
D

Dave Angel

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.

Sure. A preprocessor can be written for nearly every language. Are
you offering?
 
R

Roy Smith

JL said:
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.

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.
 
C

Chris Angelico

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
 
I

Irmen de Jong

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.

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
 
J

JL

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?
 
I

Irmen de Jong

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
 
T

Terry Reedy

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.
 
J

JL

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.
 
S

Serhiy Storchaka

15.11.13 06:57, Chris Angelico напиÑав(ла):
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.)
.... def __add__(self, other):
.... return super().__add__(other) - 0.1
....124.9
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,020
Latest member
GenesisGai

Latest Threads

Top