472,988 Members | 2,389 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,988 software developers and data experts.

Life-time of temporary variables in list comprehensions

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
4 3003
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: cognite | last post by:
This venue would surely appreciate the cool stuff being done in python on bioinformatics and python's tools for info parsing and extraction (like fulltext indexing, xml tools, parser builders,...
1
by: Johnny A. Stoa | last post by:
PRESS RELEASE I am pleased to forward to you our official press release related to the launch of our new software product Life*TYPE. Life*TYPE is an XML/SGML formatting and paginating COTS...
6
by: Paul | last post by:
In real life situation, do we ever come across a situation where we would need two base objects in an object. A snippet is worth 1000 words (: so... class Base { }; class Derived1:public Base...
6
by: jim | last post by:
Hi All, I like to know the life cycle of an ASP .NET Application( incudieng server application, such as .NET Web Service). That means from initialization to fully running and how to reboot it or...
6
by: CapMaster | last post by:
I'm having some trouble programming the game of life. In the version my teacher gave us, it involves a class with a private grid variable for the game. Here's the class she gave us: .. const int...
2
by: Sef | last post by:
On another forum I am working on getting a mod working for Half Life 2. It involves adding the gun from Portal to Half Life 2 however there is many differences between the two games. We have...
1
by: blackslither | last post by:
I had to implement Game of Life in a 3D matrix (int a) , but the complexity of my implementation is O(n^6) (6 imbricated for) and it runs very slow . Can anyone help me with an optimized version of...
0
by: Let U Know All | last post by:
Life happy life http://143life.blogspot.com/
0
by: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
0
by: Aliciasmith | last post by:
In an age dominated by smartphones, having a mobile app for your business is no longer an option; it's a necessity. Whether you're a startup or an established enterprise, finding the right mobile app...
0
tracyyun
by: tracyyun | last post by:
Hello everyone, I have a question and would like some advice on network connectivity. I have one computer connected to my router via WiFi, but I have two other computers that I want to be able to...
2
by: giovanniandrean | last post by:
The energy model is structured as follows and uses excel sheets to give input data: 1-Utility.py contains all the functions needed to calculate the variables and other minor things (mentions...
4
NeoPa
by: NeoPa | last post by:
Hello everyone. I find myself stuck trying to find the VBA way to get Access to create a PDF of the currently-selected (and open) object (Form or Report). I know it can be done by selecting :...
1
by: Teri B | last post by:
Hi, I have created a sub-form Roles. In my course form the user selects the roles assigned to the course. 0ne-to-many. One course many roles. Then I created a report based on the Course form and...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 1 Nov 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM) Please note that the UK and Europe revert to winter time on...
3
by: nia12 | last post by:
Hi there, I am very new to Access so apologies if any of this is obvious/not clear. I am creating a data collection tool for health care employees to complete. It consists of a number of...
3
SueHopson
by: SueHopson | last post by:
Hi All, I'm trying to create a single code (run off a button that calls the Private Sub) for our parts list report that will allow the user to filter by either/both PartVendor and PartType. On...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.