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

Search Discussions

  • Dave Angel at Nov 15, 2013 at 3:30 am

    On Thu, 14 Nov 2013 18:29:48 -0800 (PST), JL wrote:
    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?


    --
    DaveA
  • Chris Angelico at Nov 15, 2013 at 3:49 am

    On Fri, Nov 15, 2013 at 1:29 PM, JL wrote:
    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.

    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 at Nov 15, 2013 at 4:10 am
    In article <fae7479b-ecec-4114-9750-6595fa8c78fa@googlegroups.com>,
      JL wrote:

    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.
  • Chris Angelico at Nov 15, 2013 at 4:57 am

    On Fri, Nov 15, 2013 at 3:10 PM, Roy Smith 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
  • Serhiy Storchaka at Nov 16, 2013 at 10:02 am

    15.11.13 06:57, Chris Angelico ???????(??):
    On Fri, Nov 15, 2013 at 3:10 PM, Roy Smith 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
  • Irmen de Jong at Nov 15, 2013 at 11:20 pm

    On 15-11-2013 3:29, JL wrote:
    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
  • JL at Nov 15, 2013 at 11:36 pm
    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?

    On Friday, November 15, 2013 11:49:52 AM UTC+8, Chris Angelico wrote:
    On Fri, Nov 15, 2013 at 1:29 PM, JL wrote:

    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.


    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
  • Irmen de Jong at Nov 16, 2013 at 12:20 am

    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 at Nov 16, 2013 at 12:22 am

    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 at Nov 16, 2013 at 12:22 am

    On 15/11/2013 23: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?
    On Friday, November 15, 2013 11:49:52 AM UTC+8, Chris Angelico wrote:
    On Fri, Nov 15, 2013 at 1:29 PM, JL wrote:

    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.


    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

    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 at Nov 16, 2013 at 5:38 am

    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.
  • Mark Lawrence at Nov 16, 2013 at 11:48 am

    On 16/11/2013 05:38, JL wrote:
    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.

    No problem. It's not a matter of etiquette, it's using a tool that's
    not flawed :)


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


    Mark Lawrence

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
grouppython-list @
categoriespython
postedNov 15, '13 at 2:29a
activeNov 16, '13 at 11:48a
posts13
users8
websitepython.org

People

Translate

site design / logo © 2022 Grokbase