Need help with Python scoping rules

Steven D'Aprano steve at REMOVE-THIS-cybersource.com.au
Thu Aug 27 14:17:15 CEST 2009


On Thu, 27 Aug 2009 13:45:00 +0200, Jean-Michel Pichavant wrote:

> in foo.py:
> 
> a = 5
> b = a # works fine
> 
> class A:
>     c = 5
>     d = c # broken

Incorrect. That works fine.

>>> class A:
...     c = 5
...     d = c # not actually broken
...
>>> A.c
5
>>> A.d
5

The class is a scope, and inside the class scope, you can access local 
names. What you can't do is access the class scope from inside nested 
functions.



-- 
Steven



More information about the Python-list mailing list