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


Groups > comp.lang.python > #83632

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!newsfeed3.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail
Return-Path <rosuav@gmail.com>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.001
X-Spam-Evidence '*H*': 1.00; '*S*': 0.00; 'definitions': 0.07; 'namespace': 0.09; 'prevents': 0.09; 'subject:skip:c 10': 0.09; 'cc:addr:python-list': 0.11; 'python': 0.11; 'def': 0.12; 'jan': 0.12; 'comp': 0.16; 'easier.': 0.16; 'from:addr:rosuav': 0.16; 'from:name:chris angelico': 0.16; 'iterator': 0.16; 'leakage': 0.16; 'namespace,': 0.16; 'nesting': 0.16; 'personally,': 0.16; 'scope.': 0.16; 'subject:class': 0.16; 'subject:fails': 0.16; 'weird': 0.16; 'wrote:': 0.18; 'bit': 0.19; '>>>': 0.22; 'hack': 0.22; 'cc:addr:python.org': 0.22; 'print': 0.22; 'cc:2**0': 0.24; 'defined': 0.27; 'header:In-Reply-To:1': 0.27; 'function': 0.29; 'am,': 0.29; "doesn't": 0.30; 'subject:list': 0.30; 'message- id:@mail.gmail.com': 0.30; '"",': 0.31; '13,': 0.31; 'prints': 0.31; 'file': 0.32; 'class': 0.32; 'received:google.com': 0.35; 'keyword': 0.36; 'list': 0.37; 'fact': 0.38; 'that,': 0.38; 'john': 0.61; 'name': 0.63; 'special': 0.74; '2015': 0.84; '7:25': 0.84; 'to:none': 0.92
DKIM-Signature v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:date:message-id:subject:from:cc :content-type; bh=YMYgYtp6yBxfNnYKll/o6AqwszjHZlS9fRvFHMmKgwY=; b=0hp5kkngihAbkBcNjGAY0FjEHIrLC01vS0L3C0OYx9NkpBEdzIlTrSVqa3arcOgDT4 WT66PoWMKxSKUM3jCmSg4fMxmVeOS2wdOWH2dmGV2u3Q7+99zpuE+kLQ+DAHCu1YQh8K yzjMMSALR7zphC6rbPWeO8J9t7bxyqtzedvPTqb1CtrOnNgdbcx57jZGqr9rq+UlZGyW NYACkV6sORSlzN0BwH8gv3RFYw4+jV4ryp35belK68UAm7RE1HV1FJKTTXKdboJlRaB7 Y5zChCpacPaSMV/nR6tofZH2zkYsZ6ky/bZ8rGcJCcVmzE8et9K/vK7tHzleYzORWhd7 MH8g==
MIME-Version 1.0
X-Received by 10.50.62.104 with SMTP id x8mr8610089igr.2.1421095883312; Mon, 12 Jan 2015 12:51:23 -0800 (PST)
In-Reply-To <af9bb2fd-f0b9-4efb-874e-78f30ac65b7e@googlegroups.com>
References <af9bb2fd-f0b9-4efb-874e-78f30ac65b7e@googlegroups.com>
Date Tue, 13 Jan 2015 07:51:23 +1100
Subject Re: Namespace puzzle, list comprehension fails within class definition
From Chris Angelico <rosuav@gmail.com>
Cc "python-list@python.org" <python-list@python.org>
Content-Type text/plain; charset=UTF-8
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.17639.1421095890.18130.python-list@python.org> (permalink)
Lines 31
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1421095890 news.xs4all.nl 2920 [2001:888:2000:d::a6]:35037
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:83632

Show key headers only | View raw


On Tue, Jan 13, 2015 at 7:25 AM, John Ladasky
<john_ladasky@sbcglobal.net> wrote:
> When I am working inside the class namespace, the print function call on line 8 recognizes the name f and prints the dictionary bound to that name.
>
> However, the LIST COMPREHENSION defined inside the class namespace generates a NameError.

A list comp is defined with a function call:

>>> def f():
...  return [x*x for x in range(4)]
...
>>> dis.dis(f)
  2           0 LOAD_CONST               1 (<code object <listcomp> at
0x7fdf25981420, file "<stdin>", line 2>)
              3 LOAD_CONST               2 ('f.<locals>.<listcomp>')
              6 MAKE_FUNCTION            0
              9 LOAD_GLOBAL              0 (range)
             12 LOAD_CONST               3 (4)
             15 CALL_FUNCTION            1 (1 positional, 0 keyword pair)
             18 GET_ITER
             19 CALL_FUNCTION            1 (1 positional, 0 keyword pair)
             22 RETURN_VALUE

This prevents leakage of the iterator into the enclosing scope.
Personally, I think it's a hack to get around the fact that Python
doesn't have any concept of sub-function-scope (similar to the weird
hack in try/except); if it weren't for that, true nesting would be
easier. As it is, function definitions in class scope have a special
meaning, and that interferes a bit with list comps.

ChrisA

Back to comp.lang.python | Previous | NextPrevious 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