list comprehensions put non-names into namespaces!
Lonnie Princehouse
finite.automaton at gmail.com
Thu May 25 13:23:33 EDT 2006
List comprehensions appear to store their temporary result in a
variable named "_[1]" (or presumably "_[2]", "_[3]" etc for nested
comprehensions)
In other words, there are variables being put into the namespace with
illegal names (names can't contain brackets). Can't someone come up
with a better hack than this? How about using "_1", "_2", etc, or
actually making "_" a list of lists and using the real first, second,
third elements? This is an unexpected wrench in the works for people
trying to implement custom global namespaces.
Illustration:
class custom_namespace(dict):
def __getitem__(self, i):
print "GET", i
return dict.__getitem__(self, i)
eval("[x for x in range(10)]", custom_namespace())
More information about the Python-list
mailing list