static attribute

Fredrik Lundh fredrik at pythonware.com
Sat May 12 10:57:54 EDT 2001


Uwe Schmitt wrote:
> i tried to use a static attribute _allNames as follows:
>
>
> class Name:
>
>    _allNames={}
>
>    def __init__(self,name):
>       self.name=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:

    Name._allNames[name]=self

(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