Nested scopes and class variables

Steven Bethard steven.bethard at gmail.com
Mon Jan 31 17:21:55 CET 2005


Alex Martelli wrote:
>>>>def f(x):
> 
> ...   class C:
> ...     x = x
> ...   return C
> ... 
> 
[snip]
>
>>>>def f(x):
> 
> ...   def g():
> ...     x = x
> ...     return x
> ...   return g
> ... 
> 
[snip]
>
> See the difference?  In a function, the 'x = x' compiles into LOAD_FAST,
> STORE_FAST, which only looks at locals and nowhere else.  In a
> classbody, it compiles to LOAD_NAME, STORE_NAME, which looks at locals
> AND globals -- but still not at closure cells...

Is there a reason why the class body doesn't look at closure cells? 
That is, are there cases where this lookup scheme is preferred to one 
that checks locals, closure cells and globals?

Steve

P.S. Question disclaimer:
My questions here are founded in a curiosity about language design, and 
are not intended as an attack on Python. =)



More information about the Python-list mailing list