Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #21947

List comprehension/genexp inconsistency.

Path csiph.com!usenet.pasdenom.info!aioe.org!news.stack.nl!newsfeed.xs4all.nl!newsfeed5.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail
Return-Path <jcd@sdf.lonestar.org>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.005
X-Spam-Evidence '*H*': 0.99; '*S*': 0.00; 'python': 0.08; 'foo,': 0.09; 'nameerror:': 0.09; 'though:': 0.09; 'variables.': 0.09; '3.2,': 0.16; 'baz': 0.16; 'cliff': 0.16; 'coworkers': 0.16; 'definition,': 0.16; 'obviously,': 0.16; 'stumbled': 0.16; 'x-mailer:evolution 2.32.3': 0.16; '>>>': 0.18; 'issue.': 0.19; 'cheers,': 0.20; '(most': 0.21; 'trying': 0.21; '(but': 0.21; 'versions': 0.23; 'found,': 0.23; 'defined': 0.24; 'traceback': 0.24; 'subject:List': 0.25; 'variable': 0.28; "i'm": 0.28; 'class': 0.29; 'problem': 0.29; 'outer': 0.30; 'subject:skip:i 10': 0.30; 'list': 0.32; 'there': 0.33; 'file': 0.34; 'loop': 0.34; 'someone': 0.34; 'subject:/': 0.34; 'curious': 0.34; 'last):': 0.34; 'nested': 0.34; 'to:addr:python-list': 0.35; 'received:org': 0.36; 'issue': 0.37; 'around.': 0.37; 'enough': 0.38; 'received:192': 0.38; 'fail': 0.39; 'to:addr:python.org': 0.40; 'easy': 0.60; 'here': 0.64; 'cause': 0.67; 'strange': 0.68; 'discovered': 0.70; 'hood': 0.84
Subject List comprehension/genexp inconsistency.
From "J. Cliff Dyer" <jcd@sdf.lonestar.org>
To python-list@python.org
Content-Type text/plain; charset="UTF-8"
Date Tue, 20 Mar 2012 16:23:22 -0400
Mime-Version 1.0
X-Mailer Evolution 2.32.3
Content-Transfer-Encoding 7bit
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.12
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <http://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <http://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.841.1332275170.3037.python-list@python.org> (permalink)
Lines 38
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1332275170 news.xs4all.nl 6932 [2001:888:2000:d::a6]:58065
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:21947

Show key headers only | View raw


One of my coworkers just stumbled across an interesting issue.  I'm
hoping someone here can explain why it's happening.

When trying to create a class with a dual-loop generator expression in a
class definition, there is a strange scoping issue where the inner
variable is not found, (but the outer loop variable is found), while a
list comprehension has no problem finding both variables.

Demonstration:

>>> class Spam:
...     foo, bar = 4, 4
...     baz = dict(((x, y), x+y) for x in range(foo) for y in
range(bar))
... 
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "<stdin>", line 3, in Spam
  File "<stdin>", line 3, in <genexpr>
NameError: global name 'bar' is not defined
>>> class Eggs(object):
...     foo, bar = 4, 4
...     baz = dict([((x, y), x+y) for x in range(foo) for y in
range(bar)])
... 
>>> 

This was discovered in python 2.6.  In python 3.2, both versions fail
with the same NameError.  

Obviously, this is easy enough to work around.  I'm curious though:
What's going on under the hood to cause the nested generator expression
to fail while the list comprehension succeeds?

Cheers,
Cliff

Back to comp.lang.python | Previous | Next | Find similar | Unroll thread


Thread

List comprehension/genexp inconsistency. "J. Cliff Dyer" <jcd@sdf.lonestar.org> - 2012-03-20 16:23 -0400

csiph-web