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


Groups > comp.lang.python > #111098

Re: Nested class doesn't see class scope

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:41:16 -0600
Lines 29
Message-ID <mailman.85.1467693723.2295.python-list@python.org> (permalink)
References <577b2768$0$1606$c3e8da3$5496439d@news.astraweb.com> <CALwzidn1BNUHg5kakWNN5ibxqg73EC4ZNcbmT-HdHGTO5QanHw@mail.gmail.com>
Mime-Version 1.0
Content-Type text/plain; charset=UTF-8
X-Trace news.uni-berlin.de 8IW6vx+dOq/Ei7GBAz5vZQQl7mWi6EYetv3boVmSZ07g==
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; '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; '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; 'anyone': 0.32; 'class': 0.33; "d'aprano": 0.33; 'steven': 0.33; 'traceback': 0.33; 'file': 0.34; 'received:google.com': 0.35; 'expected': 0.35; 'but': 0.36; 'to:addr:python-list': 0.36; 'pm,': 0.36; 'subject:: ': 0.37; 'being': 0.37; '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=8yzg8NkHpejixU6bQTA+O9CBt+zXjSZsLd8lGMBcaoM=; b=mzf5ccI0i9p4s/zRSxIl9rCvWgD18m3kNwYieruUW1E/YmzKOt8G4hOi06UH1qsZPa XBRXERXj9aFm8LV1yX0e2n6lvij2rKBYMf/i04DsEJvH4YBg9OC++seTbgv2/AAp0Gae ucmMcO1176g7AZ4HVQtlGZ1AOcZ54v46u2y5WDajCvbiuASxT9h5mocImYW0OPLSpq9h 5Zzz21bBYarZs5hl2Mxc83S8F1vYJnKw6KKWjXN/M+WnI7kf1TnyLg1roaTd5fK3YM7I BQGrMv7a4FTowoiQT4YKjz77Db0R/8BdKQG2MEM7JMBxTq+djIUgszJ0dGU478DpYzQ3 akkg==
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=8yzg8NkHpejixU6bQTA+O9CBt+zXjSZsLd8lGMBcaoM=; b=AXvidvUTUvcpZLH0Orpwbk2hQUQOxQxUJkLqj4LpdY6yJzNoRnD7K7h5iQAiZZlfDt fVTBKwNb/dpkonkklkbTb08PL9qFd6xdh2dN3zA68oFM2KvhlAV8kXzrB872Ydhs91HE m/JNdUC0FNFzGHhwqwlqYsyEPz8rfZvtDnD8P0caGSKUx2HH6NMri5NfbgqyD194eiGJ iL+jPmkAflwNRMygEiVfkOzVc4lVPbMlXnNiuK7yRP6f3UsYn57zinsIdPSsPGh+kJEN 1lsaZC9ZTy0iBcVt6WPE3SnVr5+LN7Xkkk/D3GxYrzuSbEozbtonlkTSx1rmJkRVsK6F cl4g==
X-Gm-Message-State ALyK8tKMAtOnkOluwkiodulVE0Lw4jtelNUDFGSX7nmVS105NtR1QCZgUYziZZ6WlsmhBLuzJ1vZnGppeyNzgA==
X-Received by 10.202.80.78 with SMTP id e75mr7820813oib.91.1467693715678; Mon, 04 Jul 2016 21:41:55 -0700 (PDT)
In-Reply-To <577b2768$0$1606$c3e8da3$5496439d@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 <CALwzidn1BNUHg5kakWNN5ibxqg73EC4ZNcbmT-HdHGTO5QanHw@mail.gmail.com>
X-Mailman-Original-References <577b2768$0$1606$c3e8da3$5496439d@news.astraweb.com>
Xref csiph.com comp.lang.python:111098

Show key headers only | View raw


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.

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


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