Metaclasses and class variables
Kent Johnson
kent37 at tds.net
Thu Aug 4 09:47:49 EDT 2005
Christopher Subich wrote:
> Jan-Ole Esleben wrote:
>
>> class Meta(type):
>> def __new__(cls, name, bases, d):
>> d['classvar'] = []
>> return type.__new__(cls, name, bases, d)
>
>
> The problem is that __new__ is called upon object construction, not
> class definition, but you're trying to set the class variables at
> definition-time.
The metaclass __new__() is called on class creation (the class is the object being constructed) but not until after the body of the class definition is executed; the dictionary passed to __new__() contains the methods and class variables defined by the class statement.
See http://www.python.org/2.2/descrintro.html#metaclasses
Kent
More information about the Python-list
mailing list