could ildg wrote: > 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 reason for this is that the super object is a descriptor. This means that self.__super actually calls something like: type(self).__super.__get__(self) So the difference in the two is that the "in metaclass" print references the unbound super object, and the "in class A" print references the bound super object. A very simple demonstration of this: py> class C(object): ... pass ... py> C.sup = super(C) py> C.sup <super: <class 'C'>, NULL> py> C().sup <super: <class 'C'>, <C object>> py> C.sup.__get__(C()) <super: <class 'C'>, <C object>> Note that we get the "NULL" output when we ask the *class* for the "sup" object. We get the "C object" output when we ask an *instance* for the "sup" object. The example using __get__ is basically just the explicit version of the one that doesn't use __get__. HTH, STeVe -- http://mail.python.org/mailman/listinfo/python-list