What is print? A function?

Fredrik Lundh fredrik at pythonware.com
Sun Jan 23 13:09:51 EST 2005


>> The reason I thinks about this is I need to implement a debug print for my
>> program; very simple, a function/print statement that conditionally prints
>> its message whether a bool is true. Not overly complex.
>>
>> I tried this by overshadowing the print keyword, but that obviously didn't
>> work.. Is defining a two-liner function the right way to go
>
> yup.  something like this might be useful:
>
>    def printif(cond, fmt, *args):
>        if cond: print fmt % args

or, perhaps more useful in your case:

    if my_debug_flag:
        def debug(fmt, *args):
            print fmt % args
    else:
        def debug(*args): pass

    debug("hello!")
    debug("the value is %d", value)

</F> 






More information about the Python-list mailing list