Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #111124
| Path | csiph.com!fu-berlin.de!uni-berlin.de!not-for-mail |
|---|---|
| From | eryk sun <eryksun@gmail.com> |
| Newsgroups | comp.lang.python |
| Subject | Re: Nested class doesn't see class scope |
| Date | Tue, 5 Jul 2016 12:45:41 +0000 |
| Lines | 42 |
| Message-ID | <mailman.98.1467722789.2295.python-list@python.org> (permalink) |
| References | <577b2768$0$1606$c3e8da3$5496439d@news.astraweb.com> <CALwzidn1BNUHg5kakWNN5ibxqg73EC4ZNcbmT-HdHGTO5QanHw@mail.gmail.com> <mailman.85.1467693723.2295.python-list@python.org> <577b4848$0$2763$c3e8da3$76491128@news.astraweb.com> <CACL+1avVBwN2C+BRss6+C3f+vLrcf76CqpeVvCjr73Ep92ZC8w@mail.gmail.com> |
| Mime-Version | 1.0 |
| Content-Type | text/plain; charset=UTF-8 |
| X-Trace | news.uni-berlin.de fla4ybzCURi+39HWtE+ZvQGFrcame5nRkrKztCam3upA== |
| Return-Path | <eryksun@gmail.com> |
| X-Original-To | python-list@python.org |
| Delivered-To | python-list@mail.python.org |
| X-Spam-Status | OK 0.000 |
| X-Spam-Evidence | '*H*': 1.00; '*S*': 0.00; 'compiler': 0.05; 'cpython': 0.05; 'definitions': 0.07; 'implements': 0.07; '__future__': 0.09; 'attribute.': 0.09; 'definition,': 0.09; 'dict': 0.09; 'executes': 0.09; 'instruction.': 0.09; 'example:': 0.10; 'python': 0.10; 'def': 0.13; 'interpreter': 0.15; 'variables': 0.15; "'__doc__',": 0.16; '2016': 0.16; 'closures': 0.16; 'globals.': 0.16; 'metaclass': 0.16; 'received:io': 0.16; 'received:psf.io': 0.16; 'storing': 0.16; 'subject:class': 0.16; 'wrote:': 0.16; 'obviously': 0.16; 'nested': 0.18; 'creates': 0.18; '>>>': 0.20; 'do.': 0.22; 'am,': 0.23; 'import': 0.24; 'header:In-Reply-To:1': 0.24; 'separate': 0.27; 'message- id:@mail.gmail.com': 0.27; "skip:' 10": 0.28; 'declared': 0.29; 'objects': 0.29; 'classes': 0.30; 'code': 0.30; 'class': 0.33; "d'aprano": 0.33; 'steven': 0.33; 'tue,': 0.34; 'received:google.com': 0.35; 'so,': 0.35; 'to:addr:python-list': 0.36; 'subject:: ': 0.37; 'being': 0.37; "won't": 0.38; 'does': 0.39; 'to:addr:python.org': 0.40; 'share': 0.61; 'body': 0.61; 'default': 0.61; 'skip:n 10': 0.62; 'between': 0.65; 'jul': 0.72; 'dict,': 0.84; 'dict.': 0.84; 'locals': 0.84 |
| DKIM-Signature | v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:from:date:message-id:subject:to; bh=L7N4pZvSsy9tpsWDY3JV5aNMtC3yQtNGnREpPf7hqFo=; b=YnXbvZRsKyjJZhl0gRJPmH/VX4N5erDibEsLlhvwblg1/ncYktrAOn4WbdKZPU3uvS uCt/uEeHl4UANyTN4yV2yULtYGGK1EeQH/pinxD6LZyZvid6SAbyfTBp1G/gfnyTqXuF 5wtZFZij20/2Ap9A1Cng1rVd+mnIsU3IYN5P0bRFPGQF1qbuFzMR72RzFytJ3cp/pKm9 CFJdJNmppq91nQ4EcpzmNQKGxvUVi2UAsIQI1csB4YXdObn+QmYcJkbav5KgwohO6TyP udBiskdFQOLUJUSH2rr8caRkOiShrIGYyLtDHZ9dzJgbmMjEJuCY/kCzq6OzT4OK2jYD 1c+A== |
| X-Google-DKIM-Signature | v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:mime-version:in-reply-to:references:from:date :message-id:subject:to; bh=L7N4pZvSsy9tpsWDY3JV5aNMtC3yQtNGnREpPf7hqFo=; b=PKQVjapYzJbmEAiJ5SWD/anhP8UGUCrAS1uJkLTQlvJmea+FZwAEaEpr3vc5NwTBgr W/wkyfADXdjlqpJGK7wROJuQVaFxLRDGQSlpIFv43lzSLcB7h3OLPgit4ojgzXrSMN2M sQ4tKmVtVsmS5E608aVOkGd29seDPr77hVHiYoNczk4sByG44NSH2A3R/JmQ2ZkFqH2L zbYaOjQ9zZSeIljZ9lx6Ui013z/N6a0hqAxQ7kPZh4UHOxfmI0ow6JruRrQH4Tq6JX6J OVQN3fypf3AyLE2rlRye/YwPb7tYx1QY8TB1072a1a2eqgswX9eDnwoGKdclZhO/RAn5 59cw== |
| X-Gm-Message-State | ALyK8tIlY11t8tIcwMUR8RFlkicj6Kk5Qr27NHWgfIkeia7TMUwV2luIf5DnTh9rZXUNZkfzySHjl/mW+0qTkw== |
| X-Received | by 10.36.37.146 with SMTP id g140mr63880itg.58.1467722780943; Tue, 05 Jul 2016 05:46:20 -0700 (PDT) |
| In-Reply-To | <577b4848$0$2763$c3e8da3$76491128@news.astraweb.com> |
| X-BeenThere | python-list@python.org |
| X-Mailman-Version | 2.1.22 |
| 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> |
| X-Mailman-Original-Message-ID | <CACL+1avVBwN2C+BRss6+C3f+vLrcf76CqpeVvCjr73Ep92ZC8w@mail.gmail.com> |
| X-Mailman-Original-References | <577b2768$0$1606$c3e8da3$5496439d@news.astraweb.com> <CALwzidn1BNUHg5kakWNN5ibxqg73EC4ZNcbmT-HdHGTO5QanHw@mail.gmail.com> <mailman.85.1467693723.2295.python-list@python.org> <577b4848$0$2763$c3e8da3$76491128@news.astraweb.com> |
| Xref | csiph.com comp.lang.python:111124 |
Show key headers only | View raw
On Tue, Jul 5, 2016 at 5:40 AM, Steven D'Aprano
<steve+comp.lang.python@pearwood.info> wrote:
> On Tuesday 05 July 2016 14:41, Ian Kelly wrote:
>
>> Class definitions don't create closures like functions do. When Python
>> executes a class definition, the metaclass creates a dict, and then
>> the interpreter execs the class body using that dict as the locals.
>> The body of class A has one locals dict, and the body of class B has a
>> completely separate locals dict. The only way to share variables
>> between them (prior to the class objects actually being constructed)
>> is via globals.
>
>
> So, like nested functions in Python before "from __future__ import
> nested_scopes".
In Python 3.4+, the code for a class body does participate in
closures. The CPython compiler implements this using the
LOAD_CLASSDEREF instruction. However, classes don't create closures
and default to storing to the locals dict (as class attributes),
unless a name is declared global or nonlocal. Obviously writing to a
global or nonlocal won't create a class attribute. For example:
def f():
y = 0
class C:
global x
nonlocal y
x = 1
y = 2
z = 3
return types.SimpleNamespace(**locals())
>>> ns = f()
>>> x
1
>>> ns.y
2
>>> ns.C.z
3
>>> sorted(vars(ns.C))
['__dict__', '__doc__', '__module__', '__weakref__', 'z']
Back to comp.lang.python | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
Nested class doesn't see class scope Steven D'Aprano <steve@pearwood.info> - 2016-07-05 13:20 +1000
Re: Nested class doesn't see class scope Rustom Mody <rustompmody@gmail.com> - 2016-07-04 20:37 -0700
Re: Nested class doesn't see class scope Paul Rubin <no.email@nospam.invalid> - 2016-07-04 20:42 -0700
Re: Nested class doesn't see class scope Ian Kelly <ian.g.kelly@gmail.com> - 2016-07-04 22:37 -0600
Re: Nested class doesn't see class scope Ian Kelly <ian.g.kelly@gmail.com> - 2016-07-04 22:41 -0600
Re: Nested class doesn't see class scope Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2016-07-05 15:40 +1000
Re: Nested class doesn't see class scope eryk sun <eryksun@gmail.com> - 2016-07-05 12:45 +0000
Re: Nested class doesn't see class scope Ian Kelly <ian.g.kelly@gmail.com> - 2016-07-04 22:43 -0600
csiph-web