Optimization

Michael Hudson mwh21 at cam.ac.uk
Sat Oct 30 19:28:01 EDT 1999


Geir Bjarte Terum <gterum at c2i.net> writes:

> Will statements like this be optimized away using the -o program switch?
> 
> if __debug__:
>   <statements>  # Dead code in optimized version.

Yes.

> Is there any way to define similar *variables* for other purposes?

Don't think so.

> If not, as a workaround, will the following work?
> 
> __myflag__ = __debug__  # Set to 1 if always to be supported.
> 
> if __myflag__:
>   <statements>  # Dead code in non-flagged version.

Doubt it. There's no real way to enforce constants in Python.

One solution is to write

if 0: # __myflag__

because `if 0:' does allow code elimination, and then use search and
replace if the value needs changing. Not a perfect solution, but it
could work.

HTH,
Michael




More information about the Python-list mailing list