variable declaration

Duncan Booth duncan.booth at invalid.invalid
Tue Feb 8 04:43:02 EST 2005


Brian van den Broek wrote:

> Can it then be further (truly :-) ) said that
> 
> if False:
>      # thousands of lines of code here
> 
> would effect the structure of the function object's bytecode, but not 
> its behaviour when run? Or, at most, would cause a performance effect 
> due to the bytecode being bloated by thousands of line's worth of code 
> that would never get executed?
> 

Yes, but that purely an implementation detail.

if 0:
   # thousands of lines of code here

has no effect at all on the bytecode, it it optimised out entirely. 'if 
False:' is not optimised out in Python 2.4 or earlier, but might be in 
later versions.

Now, to really get your brain hurting, see what this one does:

def hurts_my_brain(v):
    if 0:     # unlike Steve's eg, ensuring that the
        global x  # nested block is never hit at runtime
    x = v

I'll give you a clue, it's not the same as your:

def hurts_my_brain(v):
    if False:     # unlike Steve's eg, ensuring that the
        global x  # nested block is never hit at runtime
    x = v

So the global statement is a wart which isn't executed at runtime, but 
behaves differently when the bytecode it doesn't generate is optimised out.



More information about the Python-list mailing list