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


Groups > comp.lang.python > #11449

Re: surprising interaction between function scope and class namespace

Path csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!gegeweb.42!gegeweb.eu!nntpfeed.proxad.net!proxad.net!feeder1-2.proxad.net!usenet-fr.net!de-l.enfer-du-nord.net!feeder2.enfer-du-nord.net!feeder.news-service.com!newsfeed.xs4all.nl!newsfeed5.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail
Return-Path <python-python-list@m.gmane.org>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.002
X-Spam-Evidence '*H*': 1.00; '*S*': 0.00; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:80.91.229.12': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'received:lo.gmane.org': 0.09; 'def': 0.15; 'explanation': 0.16; 'from:addr:behnel.de': 0.16; 'from:addr:stefan_ml': 0.16; 'from:name:stefan behnel': 0.16; 'namespace,': 0.16; 'rhs': 0.16; 'scope,': 0.16; 'scope.': 0.16; 'stumbled': 0.16; 'subject:between': 0.16; 'subject:function': 0.16; 'looked': 0.16; 'this:': 0.16; '>>>': 0.18; 'header:In-Reply-To:1': 0.22; 'incorrect': 0.23; 'variable': 0.24; 'function': 0.27; 'received:84': 0.28; 'around.': 0.29; 'stefan': 0.29; 'second': 0.29; 'class': 0.30; 'does': 0.32; 'anyone': 0.32; 'to:addr :python-list': 0.33; 'however,': 0.34; '...': 0.34; 'header:User- Agent:1': 0.34; 'assignment': 0.34; 'header:X-Complaints-To:1': 0.35; 'totally': 0.35; 'but': 0.37; 'think': 0.38; 'received:org': 0.38; 'subject:: ': 0.39; 'header:Mime-Version:1': 0.39; "couldn't": 0.39; 'to:addr:python.org': 0.39; 'case': 0.39; 'where': 0.40; 'one,': 0.40; 'results': 0.61; 'therefore,': 0.66; 'reasoning': 0.84; 'subject:class': 0.84
X-Injected-Via-Gmane http://gmane.org/
To python-list@python.org
From Stefan Behnel <stefan_ml@behnel.de>
Subject Re: surprising interaction between function scope and class namespace
Date Mon, 15 Aug 2011 11:50:58 +0200
References <j2ap58$pn0$1@dough.gmane.org>
Mime-Version 1.0
Content-Type text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding 7bit
X-Gmane-NNTP-Posting-Host dslb-084-056-020-031.pools.arcor-ip.net
User-Agent Mozilla/5.0 (X11; U; Linux x86_64; en-US; rv:1.9.2.18) Gecko/20110617 Lightning/1.0b2 Thunderbird/3.1.11
In-Reply-To <j2ap58$pn0$1@dough.gmane.org>
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.12
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <http://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 <http://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.6.1313401876.27778.python-list@python.org> (permalink)
Lines 37
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1313401876 news.xs4all.nl 23921 [2001:888:2000:d::a6]:33699
X-Complaints-To abuse@xs4all.nl
Xref x330-a1.tempe.blueboxinc.net comp.lang.python:11449

Show key headers only | View raw


Stefan Behnel, 15.08.2011 11:33:
> I just stumbled over this:
>
>  >>> A = 1
>  >>> def foo(x):
>  ...     A = x
>  ...     class X:
>  ...         a = A
>  ...     return X
>  ...
>  >>> foo(2).a
>  2
>  >>> def foo(x):
>  ...     A = x
>  ...     class X:
>  ...         A = A
>  ...     return X
>  ...
>  >>> foo(2).A
>  1
>
> Works that way in Py2.7 and Py3.3.
>
> I couldn't find any documentation on this, but my *guess* about the
> reasoning is that the second case contains an assignment to A inside of the
> class namespace, and assignments make a variable local to a scope, in this
> case, the function scope. Therefore, the A on the rhs is looked up in that
> scope as well. However, this is just a totally hand waving guess.

... and an incorrect one, as it turns out. I think I misinterpreted the 
results the wrong way around. Still:

> Does anyone have a better explanation or know of a place where this
> specific behaviour is documented?

Stefan

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


Thread

Re: surprising interaction between function scope and class namespace Stefan Behnel <stefan_ml@behnel.de> - 2011-08-15 11:50 +0200

csiph-web