Access from a class attribute
bruno.42.desthuilliers at websiteburo.invalid
Thu Jun 4 12:00:59 CEST 2009
Kless a écrit :
> Why can not to access from a class attribute to a function of that
> class Foo(object):
> attr = __class__.__name__
> attr = self.__class__.__name__
"class" is an executable statement that instanciate a new class object
and bind it to the class name in the current namespace. The class object
doesn't yet exists when the body of the class statement is eval'd, so
you can't access it, obviously - nor any of it's instances FWIW.
Also, there's nothing magical wrt/ 'self' - it's just a naming
convention for the "current instance" argument of functions that are
intented to be used as methods (Python's 'methods' being just thin
wrappers around the function, the class and the instance).
If you want to mess with the class attributes, you can either do so
after the class is created (that is, after the end of the class
statement's body), or use a custom metaclass.
More information about the Python-list