Impossible to change methods with special names of instances of new-style classes?

Bruno Desthuilliers bruno.42.desthuilliers at websiteburo.invalid
Thu Jul 10 03:39:34 EDT 2008


samwyse a écrit :
> On Jul 8, 4:56 pm, Joseph Barillari <pyt... at barillari.org> wrote:
> 
>> My question is: did something about the way the special method names are
>> implemented change for new-style classes?
> 
> Just off the top of my head, I'd guess that it's due to classes
> already having a default __call__ method,

 >>> object.__dict__.keys()
['__setattr__', '__reduce_ex__', '__new__', '__reduce__', '__str__', 
'__getattribute__', '__class__', '__delattr__', '__repr__', '__hash__', 
'__doc__', '__init__']

No __call__ method here.

> used when you instatiate.

The __call__ method used to instanciate a class is actually the 
metaclass __call__ method.

 >>> class MyType(type):
...     def __call__(self, *args, **kw):
...         print "pikaboo"
...         return type.__call__(self, *args, **kw)
...
 >>> class MyClass(object):
...     __metaclass__ = MyType
...
 >>> MyClass()
pikaboo
<__main__.MyClass object at 0x8334eec>
 >>>





More information about the Python-list mailing list