Access from a class attribute

B

Bruno Desthuilliers

Kless a écrit :
Why can not to access from a class attribute to a function of that
class?

-----------------
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,290
Messages
2,571,453
Members
48,131
Latest member
AntoniaSep

Latest Threads

Top