473,578 Members | 2,927 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

list comprehensions put non-names into namespaces!

List comprehensions appear to store their temporary result in a
variable named "_[1]" (or presumably "_[2]", "_[3]" etc for nested
comprehensions)

In other words, there are variables being put into the namespace with
illegal names (names can't contain brackets). Can't someone come up
with a better hack than this? How about using "_1", "_2", etc, or
actually making "_" a list of lists and using the real first, second,
third elements? This is an unexpected wrench in the works for people
trying to implement custom global namespaces.

Illustration:

class custom_namespac e(dict):
def __getitem__(sel f, i):
print "GET", i
return dict.__getitem_ _(self, i)

eval("[x for x in range(10)]", custom_namespac e())

May 25 '06 #1
6 1387

Lonnie> List comprehensions appear to store their temporary result in a
Lonnie> variable named "_[1]" (or presumably "_[2]", "_[3]" etc for
Lonnie> nested comprehensions)

Known issue. Fixed in generator comprehensions. Dunno about plans to fix
it in list comprehensions. I believe at some point in the future they may
just go away or become syntactic sugar for a gen comp wrapped in a list()
call.

Skip
May 25 '06 #2
sk**@pobox.com wrote:
Lonnie> List comprehensions appear to store their temporary result in a
Lonnie> variable named "_[1]" (or presumably "_[2]", "_[3]" etc for
Lonnie> nested comprehensions)

Known issue. Fixed in generator comprehensions. Dunno about plans to fix
it in list comprehensions. I believe at some point in the future they may
just go away or become syntactic sugar for a gen comp wrapped in a list()
call.


The latter, starting in Python 3.0. It won't be fixed before Python
3.0 because it has the potential to break existing 2.x code. From PEP
289:

"""List comprehensions also "leak" their loop variable into the
surrounding scope. This will also change in Python 3.0, so that the
semantic definition of a list comprehension in Python 3.0 will be
equivalent to list(<generator expression>). Python 2.4 and beyond
should issue a deprecation warning if a list comprehension's loop
variable has the same name as a variable used in the immediately
surrounding scope."""

Source: http://www.python.org/dev/peps/pep-0289/

Also mentioned in PEP 3100.

Doesn't look like the deprecation warning was ever implemented for 2.4,
though. On my 2.4.3:
def f(): [x for x in range(10)]
print x f() 9 # no warning yet..


2.5 is in alpha now, hopefully the warning will be added.

--Ben

May 26 '06 #3
Ben Cartwright schrieb:
sk**@pobox.com wrote:
Lonnie> List comprehensions appear to store their temporary result in a
Lonnie> variable named "_[1]" (or presumably "_[2]", "_[3]" etc for
Lonnie> nested comprehensions)

Known issue. Fixed in generator comprehensions. Dunno about plans to fix
it in list comprehensions. I believe at some point in the future they may
just go away or become syntactic sugar for a gen comp wrapped in a list()
call.


The latter, starting in Python 3.0. It won't be fixed before Python
3.0 because it has the potential to break existing 2.x code. From PEP
289:


That is a different beast. Lonnie is after the temporary list variables
created, with otherwise illegal names in python.

Diez
May 26 '06 #4
sk**@pobox.com wrote:
Lonnie> List comprehensions appear to store their temporary result in a
Lonnie> variable named "_[1]" (or presumably "_[2]", "_[3]" etc for
Lonnie> nested comprehensions)

Known issue. Fixed in generator comprehensions. Dunno about plans to fix
it in list comprehensions. I believe at some point in the future they may
just go away or become syntactic sugar for a gen comp wrapped in a list()
call.


Point of information, would this be the interpreter putting
the result of its last calculation in _ ?

Python 2.4.2 (#1, Jan 23 2006, 21:24:54)
[GCC 3.3.4] on linux2
Type "help", "copyright" , "credits" or "license" for more
information.
[2*x for x in range(5)] [0, 2, 4, 6, 8] _[4]

8

Mel.
May 26 '06 #5

"Mel Wilson" <mw********@sym patico.ca> wrote in message
news:7h******** ***********@new s20.bellglobal. com...
Point of information, would this be the interpreter putting
the result of its last calculation in _ ?


Yes, in interactive mode (but not in batch mode) it binds the valid name
'_' to the result of statement expressions.
[2*x for x in range(5)] [0, 2, 4, 6, 8] _[4]

8


which here is the list.

Terry Jan Reedy

May 26 '06 #6
In article <ma************ *************** ************@py thon.org>,
"Terry Reedy" <tj*****@udel.e du> wrote:
"Mel Wilson" <mw********@sym patico.ca> wrote in message
news:7h******** ***********@new s20.bellglobal. com...
Point of information, would this be the interpreter putting
the result of its last calculation in _ ?


Yes, [ ... ]


No, actually. It's an implementation detail of list comps.
May 26 '06 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
1629
by: Elaine Jackson | last post by:
List comprehensions don't work the way you intuitively expect them to work. I realize many people have no intuitions about how list comprehensions 'should' work, so if you recognize yourself in this description, feel free to go back to whatever you were doing before. If you're still here, though, I invite you to consider the following...
35
2951
by: Moosebumps | last post by:
Does anyone here find the list comprehension syntax awkward? I like it because it is an expression rather than a series of statements, but it is a little harder to maintain it seems. e.g. you could do: result = for element in list: if element == 'blah':
24
3338
by: Mahesh Padmanabhan | last post by:
Hi, When list comprehension was added to the language, I had a lot of trouble understanding it but now that I am familiar with it, I am not sure how I programmed in Python without it. Now I see that generator expressions have been added to the language with 2.4 and I question the need for it. I know that it allows for lazy evaluation...
9
2349
by: Neuruss | last post by:
I have a doubt regarding list comprehensions: According to Mark Lutz in his book Learning Pyhon: "...there is currently a substantial performance advantage to the extra complexity in this case: based on tests run under Python 2.2, map calls are roughly twice as fast as equivalent for loops, and list comprehensions are usually very slightly...
42
2600
by: Alan McIntyre | last post by:
Hi all, I have a list of items that has contiguous repetitions of values, but the number and location of the repetitions is not important, so I just need to strip them out. For example, if my original list is , I want to end up with . Here is the way I'm doing this now: def straightforward_collapse(myList):
23
2243
by: Mike Meyer | last post by:
Ok, we've added list comprehensions to the language, and seen that they were good. We've added generator expressions to the language, and seen that they were good as well. I'm left a bit confused, though - when would I use a list comp instead of a generator expression if I'm going to require 2.4 anyway? Thanks, <mike --
30
3454
by: Steven Bethard | last post by:
George Sakkis wrote: > "Steven Bethard" <steven.bethard@gmail.com> wrote: >> Dict comprehensions were recently rejected: >> http://www.python.org/peps/pep-0274.html >> The reason, of course, is that dict comprehensions don't gain you >> much at all over the dict() constructor plus a generator expression, >> e.g.: >> dict((i,...
7
1607
by: Steven Bethard | last post by:
Tom Anderson <twic@urchin.earth.li> wrote: > Sounds good. More generally, i'd be more than happy to get rid of list > comprehensions, letting people use list(genexp) instead. That would > obviously be a Py3k thing, though. Alex Martelli wrote: > I fully agree, but the BDFL has already (tentatively, I hope) > Pronounced that the form will...
6
2155
by: Heiko Wundram | last post by:
Hi all! The following PEP tries to make the case for a slight unification of for statement and list comprehension syntax. Comments appreciated, including on the sample implementation. === PEP: xxx Title: Unification of for-statement and list-comprehension syntax
31
1195
by: John Salerno | last post by:
I posted this code last night in response to another thread, and after I posted it I got to wondering if I had misused the list comprehension. Here's the two examples: Example 1: -------------------- def compress(s): new = for c in s:
0
8126
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. ...
0
8291
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...
1
7874
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
8150
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...
0
6524
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...
1
5664
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
3801
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1391
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
1114
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...

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.