473,666 Members | 2,058 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 3051
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...@gmai l.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
1714
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, graphic display, realtime simulation and communities tools like pygame and ...) It's in San Diego February 9-12, 2004. "Session presentations are 45 or 90 minutes long, and tutorials are either a half-day (3 hours) or a full day (6 hours)."
1
1560
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 engine. The engine has been part of our XML/SGML content repository, Life*CDM, for years but is now also available as a stand-alone product. The press release can be read at
6
2827
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
2630
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 shut it down. Including how to establish the running environment( current working folder, ...etc) for each ASP .NET application. Because in my ASP .NET application, there are a lot of modules. Some were developed by using VC#, others using...
6
4779
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 maxrow = 20, maxcol = 60; // grid dimensions class Life { public: Life (); void initialize(); void print(); void update(); private:
2
1883
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 everything working other than being able to go through the portal themselves. Normal objects can go through but enemies and the player cannot. I am convinced its either the startup DLLs or the some type of global actors script. If anyone is interested...
1
2176
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 Game of life , even with a 2D matrix . Or how the Game of Life in general could be optimized ?
0
938
by: Let U Know All | last post by:
Life happy life http://143life.blogspot.com/
0
8356
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8869
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8781
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8639
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7386
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5664
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4368
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2771
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
1775
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.