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


Groups > comp.lang.python > #83629

Re: Namespace puzzle, list comprehension fails within class definition

Path csiph.com!usenet.pasdenom.info!gegeweb.org!aioe.org!news.stack.nl!newsfeed.xs4all.nl!newsfeed2a.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail
Return-Path <ethan@stoneleaf.us>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.002
X-Spam-Evidence '*H*': 1.00; '*S*': 0.00; 'from:addr:ethan': 0.09; 'from:addr:stoneleaf.us': 0.09; 'from:name:ethan furman': 0.09; 'lookup': 0.09; 'message-id:@stoneleaf.us': 0.09; 'subject:skip:c 10': 0.09; '~ethan~': 0.09; 'comp': 0.16; 'filename:fname piece:signature': 0.16; 'loop.': 0.16; 'namespace,': 0.16; 'namespace;': 0.16; 'skips': 0.16; 'subject:class': 0.16; 'subject:fails': 0.16; 'unnamed': 0.16; 'workaround:': 0.16; 'wrote:': 0.18; 'header:User-Agent:1': 0.23; 'header:In-Reply- To:1': 0.27; 'subject:list': 0.30; 'class': 0.32; 'actual': 0.34; 'charset:us-ascii': 0.36; 'list': 0.37; 'to:addr:python-list': 0.38; 'pm,': 0.38; 'to:addr:python.org': 0.39; 'john': 0.61; 'received:173': 0.61; 'making': 0.63; 'name': 0.63; 'effectively': 0.66
Date Mon, 12 Jan 2015 12:40:13 -0800
From Ethan Furman <ethan@stoneleaf.us>
User-Agent Mozilla/5.0 (X11; Linux x86_64; rv:24.0) Gecko/20100101 Thunderbird/24.5.0
MIME-Version 1.0
To python-list@python.org
Subject Re: Namespace puzzle, list comprehension fails within class definition
References <af9bb2fd-f0b9-4efb-874e-78f30ac65b7e@googlegroups.com>
In-Reply-To <af9bb2fd-f0b9-4efb-874e-78f30ac65b7e@googlegroups.com>
Content-Type multipart/signed; micalg=pgp-sha1; protocol="application/pgp-signature"; boundary="GVptfGemR09nr9IApsRHkfkIjUcTfv2Tm"
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.15
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <https://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 <https://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.17638.1421095249.18130.python-list@python.org> (permalink)
Lines 48
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1421095249 news.xs4all.nl 2953 [2001:888:2000:d::a6]:55029
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:83629

Show key headers only | View raw


[Multipart message — attachments visible in raw view] - view raw

On 01/12/2015 12:25 PM, John Ladasky wrote:
> d = {0:"a", 1:"b", 2:"c", 3:"d"}
> e = [d[x] for x in (0,2)]
> 
> class Foo:
>     f = {0:"a", 1:"b", 2:"c", 3:"d"}
>     print(f)
>     g = [f[x] for x in (0,2)]

In Foo 'f' is part of an unnamed namespace; the list comp 'g' has its own namespace, effectively making be a nonlocal;
class name lookup skips nonlocal namespaces.

Workaround:  use an actual for loop.

--
~Ethan~

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


Thread

Namespace puzzle, list comprehension fails within class definition John Ladasky <john_ladasky@sbcglobal.net> - 2015-01-12 12:25 -0800
  Re: Namespace puzzle, list comprehension fails within class definition Ethan Furman <ethan@stoneleaf.us> - 2015-01-12 12:40 -0800
    Re: Namespace puzzle, list comprehension fails within class definition John Ladasky <john_ladasky@sbcglobal.net> - 2015-01-12 12:49 -0800
    Re: Namespace puzzle, list comprehension fails within class definition Steven D'Aprano <steve@pearwood.info> - 2015-01-13 04:49 +0000
      Re: Namespace puzzle, list comprehension fails within class definition Ethan Furman <ethan@stoneleaf.us> - 2015-01-12 22:00 -0800
  Re: Namespace puzzle, list comprehension fails within class definition John Ladasky <john_ladasky@sbcglobal.net> - 2015-01-12 12:43 -0800
  Re: Namespace puzzle, list comprehension fails within class definition Chris Angelico <rosuav@gmail.com> - 2015-01-13 07:51 +1100

csiph-web