problem with 'global'

Gabriel Genellina gagsl-py2 at yahoo.com.ar
Mon Jan 21 15:36:19 EST 2008


En Mon, 21 Jan 2008 17:36:29 -0200, Duncan Booth  
<duncan.booth at invalid.invalid> escribi�:

> Marc 'BlackJack' Rintsch <bj_666 at gmx.net> wrote:
>
>> On Mon, 21 Jan 2008 17:08:46 -0200, Gabriel Genellina wrote:
>>
>>> The future statement is another example, even worse:
>>>
>>> if 0:
>>>      from __future__ import with_statement
>>>
>>> with open("xxx") as f:
>>>      print f
>>
>> In Python >=2.5 it's a compile time error if that import is not the very
>> first statement in a source file.
>>
> That doesn't appear to be the case. With Python 2.5.1 the example
> Gabriel quoted will compile and run.

Yes, but now I've noticed that the 0 has some magic. The code above works  
with 0, 0.0, 0j and ''. Using None, False, () or [] as the condition, will  
trigger the (expected) syntax error.

Mmm, it may be the peephole optimizer, eliminating the dead branch. This  
function has an empty body:

def f():
   if 0: print "0"
   if 0.0: print "0.0"
   if 0j: print "0j"
   if '': print "empty"

py> dis.dis(f)
   5           0 LOAD_CONST               0 (None)
               3 RETURN_VALUE

The other false values tested (False, None, () and []) aren't optimized  
out.

-- 
Gabriel Genellina




More information about the Python-list mailing list