Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #111099
| Path | csiph.com!fu-berlin.de!uni-berlin.de!not-for-mail |
|---|---|
| From | Ian Kelly <ian.g.kelly@gmail.com> |
| Newsgroups | comp.lang.python |
| Subject | Re: Nested class doesn't see class scope |
| Date | Mon, 4 Jul 2016 22:43:44 -0600 |
| Lines | 33 |
| Message-ID | <mailman.86.1467693868.2295.python-list@python.org> (permalink) |
| References | <577b2768$0$1606$c3e8da3$5496439d@news.astraweb.com> <CALwzidn1BNUHg5kakWNN5ibxqg73EC4ZNcbmT-HdHGTO5QanHw@mail.gmail.com> <CALwzidn3enXM5GF03ToS4QUzvdkzZMCQL9pih7gCXySkVEC1Bw@mail.gmail.com> |
| Mime-Version | 1.0 |
| Content-Type | text/plain; charset=UTF-8 |
| X-Trace | news.uni-berlin.de cSLc+gZ9/z9e0NEaUbfkIARZ0iMNZ+2GSLMeLKifPgVQ== |
| Return-Path | <ian.g.kelly@gmail.com> |
| X-Original-To | python-list@python.org |
| Delivered-To | python-list@mail.python.org |
| X-Spam-Status | OK 0.004 |
| X-Spam-Evidence | '*H*': 0.99; '*S*': 0.00; 'definitions': 0.07; 'definition,': 0.09; 'dict': 0.09; 'executes': 0.09; 'nameerror:': 0.09; 'python': 0.10; 'interpreter': 0.15; 'variables': 0.15; '2016': 0.16; '999': 0.16; '9:20': 0.16; 'closures': 0.16; 'fancy': 0.16; 'globals.': 0.16; 'invalid.': 0.16; 'metaclass': 0.16; 'received:io': 0.16; 'received:psf.io': 0.16; 'subject:class': 0.16; 'wrote:': 0.16; 'obviously': 0.16; 'creates': 0.18; 'do.': 0.22; '"",': 0.22; 'suppose': 0.22; 'defined': 0.23; '(most': 0.24; 'header:In-Reply-To:1': 0.24; 'mon,': 0.24; 'separate': 0.27; 'message-id:@mail.gmail.com': 0.27; 'var': 0.27; 'actual': 0.28; 'objects': 0.29; 'creating': 0.30; 'anyone': 0.32; 'class': 0.33; "d'aprano": 0.33; 'steven': 0.33; 'traceback': 0.33; 'file': 0.34; 'received:google.com': 0.35; 'could': 0.35; 'something': 0.35; 'expected': 0.35; 'but': 0.36; 'to:addr:python-list': 0.36; 'pm,': 0.36; 'subject:: ': 0.37; 'being': 0.37; 'does': 0.39; 'to:addr:python.org': 0.40; 'share': 0.61; 'body': 0.61; 'between': 0.65; 'jul': 0.72; 'construction': 0.72; 'dict,': 0.84; 'dict.': 0.84; 'locals': 0.84; 'succeeds': 0.84; 'to:name:python': 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=SzaMwtOONrroWp8i16RxTq3mgVhOqPcmADsOl5hsFz8=; b=EnICVzfWsKQ29LxVoAHshH8QQR0RRKM7iM/DWJd/av/vPpsFjsRPcy4GNpAXFCIVLm Q5A6Ww+xhvdxLTPEvRnNLpNVmRG1U1IOnEeHMDV5S7OstZMG2wW2UE4DfQXTpN59Oa95 HxsGdbgeXDQ80soY7lnG5Qa3nCKpNbl16/HSkPJgggNSdN2s1vOdLexwBOSHrFvr5Y+B bzpNZAiVfas+4oEhfNcRutQ8EW4oMy/ee4Ev866H+/QjbkgvlQXVXvQWj3XV0eXAkCDm ZITer5IEzIutJ2awoBG875Dnx6rjuIhSkqjP4T115LQzBghRG3emoxPB/mizTfgqouBs uyLA== |
| 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=SzaMwtOONrroWp8i16RxTq3mgVhOqPcmADsOl5hsFz8=; b=Ka81+P+KJttdG383A8znkI6II1qWiL4+ti74F3lvvOSY8ro6kKKwtYMi3yfMHc9c2R lQLQ63ln7f8Zut4g4AEXysgQXeDkHm0xsy4+XyJCu5HA0Kfp7aV2PAd/XYintaUmmuKj uSzibcf0a65grVeN+Bb4QOQY6aZ1GiZplj8Qa0EJ7KgTQXq8d77dX0+6IVoklN29qEL/ I2g28DNDsS0ALQbndf6HD4AUAFvNx2qQLC57UHQrGUxhPD0eHlmcxQWGtD8TXOZsetWh 2yIdkxMzSKiWN3YnRH9km0QHApQ8Kwi8dliVJ3CIZbs80bkVvWkOuFag7Yk8zS5Z/Y/4 4Xhw== |
| X-Gm-Message-State | ALyK8tLKv1sFpuOmyhU5/Tjm7ItgiHttNSatZZKH70vf2ZBO4sBvlnai7XMXdOTeTvhA3LsnnK4Ws1CsYxPQtg== |
| X-Received | by 10.202.181.137 with SMTP id e131mr9031177oif.144.1467693864267; Mon, 04 Jul 2016 21:44:24 -0700 (PDT) |
| In-Reply-To | <CALwzidn1BNUHg5kakWNN5ibxqg73EC4ZNcbmT-HdHGTO5QanHw@mail.gmail.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 | <CALwzidn3enXM5GF03ToS4QUzvdkzZMCQL9pih7gCXySkVEC1Bw@mail.gmail.com> |
| X-Mailman-Original-References | <577b2768$0$1606$c3e8da3$5496439d@news.astraweb.com> <CALwzidn1BNUHg5kakWNN5ibxqg73EC4ZNcbmT-HdHGTO5QanHw@mail.gmail.com> |
| Xref | csiph.com comp.lang.python:111099 |
Show key headers only | View raw
On Mon, Jul 4, 2016 at 10:41 PM, Ian Kelly <ian.g.kelly@gmail.com> wrote: > On Mon, Jul 4, 2016 at 9:20 PM, Steven D'Aprano <steve@pearwood.info> wrote: >> I got this in Python 3.6: >> >> >> py> class A: >> ... var = 999 >> ... print(var) # succeeds >> ... class B: >> ... x = var >> ... >> 999 >> Traceback (most recent call last): >> File "<stdin>", line 1, in <module> >> File "<stdin>", line 3, in A >> File "<stdin>", line 4, in B >> NameError: name 'var' is not defined >> >> >> I expected that `var` would be available during the construction of B, just >> as it was available inside A, but not to methods inside B. Obviously my >> expectations are invalid. Can anyone explain the actual behaviour? > > 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. Or I suppose one could write a metaclass that does something fancy when creating the dicts.
Back to comp.lang.python | Previous | Next — Previous 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