if(debug), optimized away?

Yigal Duppen yduppen at xs4all.nl
Thu Jul 25 14:39:32 EDT 2002


> if(debug):
>    if(items > 9):
>      print "fatal error..."
>    do_some_check_foo(42)
>    ...
>
> If I set debug=0, is this code optimized away as in most
> C/C++?? compilers?

I doubt it.
However, if you use the (predefined) __debug__, it does get optimized away 
(at least, I think so).

if __debug__:
        # do stuff

Note that you cannot set __debug__; it is always true, except when running 
python with the -O flag.

Furthermore, for checks such as these it is better to use the 'assert' 
keyword. This too is removed when python -O is used.

assert items > 9, "Fatal error, too many items"

YDD
-- 
.sigmentation fault



More information about the Python-list mailing list