help redefining print statement

Quinn Dunkan quinn at pfennig.ugcs.caltech.edu
Thu Nov 18 02:07:15 CET 1999


On Wed, 17 Nov 1999 20:26:59 GMT, curtin at my-deja.com <curtin at my-deja.com> wrote:
>hi,
>i'd like to redefine the "print" statement in a module. essentially
>i'm looking to do a  "#define like" replacement on a module level.
>
>i'd add at the top of the module:
>
>if debug:
>    TestingPrint=print
>else:
>    TestingPrint=eval("#") or TestingPrint="#" or ...
>
>TestingPrint "blah, blah %d, %s" % (i, s)
>
>yes, i'd like to not make the function call overhead in the non-debug
>case.
>
>i've tried a few different ways to incorporate this functionality,
>and scrounged around deja without any success.

You can't redefine print, it's a reserved work built into the language.  My
usual tactic is to use my dprint() function which checks for the debug
variable, and I don't worry about function call overhead.  But if you've got
one it in a tight loop or something, you could do:

if __debug__:
    print 'debugging info'

When python is run with -O, it won't compile things under if __debug__ (see
ref manual 6.2).

maybe-tim-could-write-a-function-that-comments-out-its-caller




More information about the Python-list mailing list