Ensure unwanted names removed in class definition

Peter Otten __peter__ at web.de
Thu Aug 13 07:44:39 CEST 2015


Ben Finney wrote:

> Peter Otten <__peter__ at web.de> writes:
> 
>> I would probably use a generator expression. These don't leak names:
> 
> That's an unexpected inconsistency between list comprehensions versus
> generator expressions, then. Is that documented explicitly in the Python
> 2 documentation?

https://docs.python.org/2/reference/expressions.html

has one sentence

"""
Note that the comprehension is executed in a separate scope, so names 
assigned to in the target list don’t “leak” in the enclosing scope.
"""

-- which is wrong unless I'm misunderstanding something, but

https://docs.python.org/2.4/whatsnew/node4.html

clearly states

"""
Generator expressions differ from list comprehensions in various small ways. 
Most notably, the loop variable (obj in the above example) is not accessible 
outside of the generator expression. List comprehensions leave the variable 
assigned to its last value; future versions of Python will change this, 
making list comprehensions match generator expressions in this respect.
"""




More information about the Python-list mailing list