A newbie metaclass question

could ildg could.net at gmail.com
Sun May 22 06:15:45 EDT 2005


When I try to learn metaclass of python by article at this place:
http://www.python.org/2.2/descrintro.html#metaclasses,
I changed the autosuper example a little as below:
<code>
class autosuper(type):
    def __init__(cls,name,bases,dict):
        super(autosuper,cls).__init__(name,bases,dict)
        setattr(cls,"_%s__super" % name, super(cls))
        print "in metaclass: ", super(cls)
        
class A:
    __metaclass__ = autosuper
    def meth(self):
        print "in class A: ", self.__super
                
a=A()
a.meth() 
</code>
The result is as below:
in metaclass:  <super: <class 'A'>, NULL>
in class A:  <super: <class 'A'>, <A object>>

The 2 line are not the same. Why? Is this because that instance a has not
been created when the __init__ of the metaclass is called? If the cls in the
__init__ method is not an instance of A, what is it?

The metaclass of python is kinda difficult for me. Thanks for your help.



More information about the Python-list mailing list