static attribute

Fredrik Lundh fredrik at
Sat May 12 16:57:54 CEST 2001

Uwe Schmitt wrote:
> i tried to use a static attribute _allNames as follows:
> class Name:
>    _allNames={}
>    def __init__(self,name):
>      _allNames[name]=self
> X=Name("Xclass")
> but i get NameError.... whats wrong ?

scoping rules.  class names belong to the class object, and
are not visible inside methods.

use this instead:


(you can also use self._allNames in this case, since you're
modifying the dictionary object instead of replacing it)

Cheers /F

More information about the Python-list mailing list