[Tutor] ifdef in python

Albert-Jan Roskam fomcl at yahoo.com
Sat Oct 24 21:49:13 CEST 2009


Isn't it better to use
if __debug__:

I thought such an if statement always evaluated to False when the python program was run in OPTIMIZED (-o) mode?

Cheers!!
Albert-Jan

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Before you criticize someone, walk a mile in their shoes, that way 
when you do criticize them, you're a mile away and you have their shoes! 
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~


--- On Wed, 10/21/09, Wayne <srilyk at gmail.com> wrote:

> From: Wayne <srilyk at gmail.com>
> Subject: Re: [Tutor] ifdef in python
> To: "Lie Ryan" <lie.1296 at gmail.com>
> Cc: tutor at python.org
> Date: Wednesday, October 21, 2009, 11:37 PM
> 
> 
> 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:   spam2except
> NameError:
> 
>    def spam2():      
> pass
> Because if DEBUG isn't declared you'll
> throw a NameError
> 
> -----Inline Attachment Follows-----
> 
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/mailman/listinfo/tutor
> 


      


More information about the Tutor mailing list