static attribute

Alex Martelli aleaxit at yahoo.com
Sat May 12 21:34:21 CEST 2001


"Uwe Schmitt" <schmitt at num.uni-sb.de> wrote in message
news:9djvum$58n61$1 at hades.rz.uni-sb.de...
    ...
> |>     _allNames[name]=self
    ...
> | self._allNames
>
> no. as i said i want a *static* attribute. Name._allNames works
> fine. thanks for the answers.

Since nothing is bound to key '_allNames' in self.__dict__, writing
self._allNames accesses the same, identical object as writing
Name._allNames will.  It would be different if you were _rebinding_
that attribute, but you aren't -- just *modifying* it: it keeps the
same identity, just changes state.

It's ok if you find Name._allNames stylistically preferable (it's
clearer and more immediate to understand that it's a class
attribute that's being accessed and modified!), but be aware
that in this code the semantics of using self._allNames instead
are identical... as long as no class inherits from Name and wants
to 'override' its attribute '_allNames' -- and if you ever do have
such a peculiar need, all the more peculiar because the leading
underline suggests it's an attribute intended for private special
use by the class, then if self._allNames was always used it is
quite likely that things will work more smoothly... but it's most
likely not worth worrying about, either way.  It may even be
worthwhile to go all the way to writing self.__class__._allNames
to get completely explicit class-centered rather than instance-
centered behavior AND yet keep 'overridability', sometimes...


Alex






More information about the Python-list mailing list