[Tutor] ifdef in python

Wayne srilyk at gmail.com
Wed Oct 21 23:37:35 CEST 2009


On Wed, Oct 21, 2009 at 3:29 PM, Lie Ryan <lie.1296 at gmail.com> wrote:

> Lizhi Yang wrote:
>
>> Is there anything similar to ifdef statement in C or C++ in python?
>>
>
> Not really, but python's regular if can be used for a similar purpose:
>
> DEBUG = True
>
> if DEBUG:
>    import logging
>    def foo():
>        # debug version
> else:
>    def foo():
>        # normal version
>
> def main():
>    foo()
>
> if __name__ == '__main__':
>    main()
>
> or something like that.


Really a try/except block would actually work

def spam1():
    pass

try:
   spam2
except NameError:
   def spam2():
       pass

Because if DEBUG isn't declared you'll throw a NameError
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20091021/f8724832/attachment.htm>


More information about the Tutor mailing list