Generator Comprehensions

gbreed at cix.compulink.co.uk gbreed at cix.compulink.co.uk
Wed Jan 30 09:55:20 EST 2002


Tim Peters wrote:

> That's not an excuse, it was the design.  Python didn't even have nested
> scopes (in 2.2's sense) when listcomps were introduced, and it would 
> have
> been horribly inconsistent if one and only one binding construct did not
> make its target name(s) local wrt to its scope.  Had nested scopes been
> there at the time, it would have been more palatable to say that 
> listcomps
> define a new scope -- but that would have been a surprise of a different
> kind (not to people coming from FP languages, but Python is not an FP
> language, and won't become one before Guido's death <wink>).

In 2.0,

>>> map(lambda x:x+1, range(10))
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
>>> x
Traceback (innermost last):
  File "<interactive input>", line 1, in ?
NameError: There is no variable named 'x'
>>> [x+1 for x in range(10)]
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
>>> x
9
>>> 

That looks horribly inconsistent to me.


                     Graham
                     
          (<http://www.microtonal.co.uk/>




More information about the Python-list mailing list