By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,318 Members | 2,380 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,318 IT Pros & Developers. It's quick & easy.

Life-time of temporary variables in list comprehensions

P: n/a
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?

Thanks,
Geoffrey

Oct 23 '07 #1
Share this Question
Share on Google+
4 Replies


P: n/a
On Tue, 2007-10-23 at 17:02 +0000, beginner wrote:
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
>"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?
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,

--
Carsten Haese
http://informixdb.sourceforge.net
Oct 23 '07 #2

P: n/a
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
Oct 23 '07 #3

P: n/a
On Tue, 23 Oct 2007 17:02:48 +0000, beginner wrote:
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.
--
Steven.
Oct 23 '07 #4

P: n/a
On Oct 23, 12:02 pm, beginner <zyzhu2...@gmail.comwrote:
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?

Thanks,
Geoffrey
I see.

Thanks for everyone's help!

Oct 23 '07 #5

This discussion thread is closed

Replies have been disabled for this discussion.