Path: csiph.com!fu-berlin.de!uni-berlin.de!not-for-mail From: eryk sun 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: References: <577b2768$0$1606$c3e8da3$5496439d@news.astraweb.com> <577b4848$0$2763$c3e8da3$76491128@news.astraweb.com> Mime-Version: 1.0 Content-Type: text/plain; charset=UTF-8 X-Trace: news.uni-berlin.de fla4ybzCURi+39HWtE+ZvQGFrcame5nRkrKztCam3upA== Return-Path: 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 List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-Mailman-Original-Message-ID: X-Mailman-Original-References: <577b2768$0$1606$c3e8da3$5496439d@news.astraweb.com> <577b4848$0$2763$c3e8da3$76491128@news.astraweb.com> Xref: csiph.com comp.lang.python:111124 On Tue, Jul 5, 2016 at 5:40 AM, Steven D'Aprano 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']