Life-time of temporary variables in list comprehensions

B

beginner

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

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?

Thanks,
Geoffrey
 
C

Carsten Haese

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]

"c='B'" is invalid syntax. Maybe you mean "c=='B'". That doesn't make
much sense, but at least it's correct syntax.
print c


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?

The only way to keep c from being overwritten currently is to avoid
using a list comprehension. Generator expressions don't "leak" their
iteration variable to the outside, so you can write this instead:

list(1.0 if c=='A' else c=='B' for c in ab)

HTH,
 
D

Diez B. Roggisch

beginner said:
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

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
 
S

Steven D'Aprano

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.

That's right.
My question is why is this and is there any way to make c local to list
comp?

It was a design decision that is now regretted. There is no way for you
to make it local to the list comp except to wait for a new version of
Python that behaves in the way you want.

Or you can look at generator expressions, which are written just like
list comprehensions except with round brackets (). They are not quite the
same thing, but you can often use one in place of the other.
 
B

beginner

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

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?

Thanks,
Geoffrey

I see.

Thanks for everyone's help!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,014
Latest member
BiancaFix3

Latest Threads

Top