if(debug), optimized away?

Chris Liechti cliechti at gmx.net
Thu Jul 25 14:25:11 EDT 2002


marvelan at hotmail.com (M) wrote in 
news:c0abefc3.0207251004.22b21b3c at posting.google.com:

> I would like to use something like this when running in
> debug mode:
> 
> 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?

no. how could it, you can change the value of "debug" at runtime...

> The reason for asking is that I would like to have A LOT of
> debugging code and would like to avoid the performance penalty
> when not runing in debug mode.

you can use "assert" which cannot do all what you do in the above example, 
but it is optimized away when running "python -O"

> Are there other, perhaps more Pythonish, ways of doing the same
> thing?

yes, exceptions. if something goes wrong you'll get an exception which you 
can handle appropriately. this is a high level language - you won't get 
coredumps etc. like in C/C++ so there is no need to check every bit. you 
can handle the exception at a higher layer and recover from there.

chris

-- 
Chris <cliechti at gmx.net>




More information about the Python-list mailing list