Life-time of temporary variables in list comprehensions
Diez B. Roggisch
deets at nospam.web.de
Tue Oct 23 13:20:57 EDT 2007
More information about the Python-list mailing list
Tue Oct 23 13:20:57 EDT 2007
- Previous message (by thread): Life-time of temporary variables in list comprehensions
- Next message (by thread): Life-time of temporary variables in list comprehensions
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
beginner schrieb: > Hi All, > > If I have a list comprehension: > > ab=["A","B"] > c = "ABC" > [1.0 if c=='A' else c='B' for c in ab] > print c > >>> "B" > > My test shows that if c is not defined before the list comprehension, > it will be created in the list comprehension; if it is defined before > the list comprehension, the value will be overwritten. In other words, > temp variables are not local to list comprehensions. > > My question is why is this and is there any way to make c local to > list comp? Unfortunately, not as such. They do leak their variable names. But what you can do ist to create generator expressions (which don't leak) and put a list around them: list(1.0 if c == 'A' else c="B" for c in ab) See also http://www.python.org/dev/peps/pep-0289/ Diez
- Previous message (by thread): Life-time of temporary variables in list comprehensions
- Next message (by thread): Life-time of temporary variables in list comprehensions
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
More information about the Python-list mailing list