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

cokofreedom at gmail.com cokofreedom at gmail.com
Wed Jul 9 07:08:30 EDT 2008


>
> My question is: did something about the way the special method names are
> implemented change for new-style classes?
>

>>> class old:
	pass

>>> class new(object):
	pass

>>> testone = old()
>>> testone.__call__ = lambda : 33
>>> testone()
33
>>> testtwo = new()
>>> testtwo.__call__ = lambda : 33
>>> testtwo()

Traceback (most recent call last):
  File "<pyshell#30>", line 1, in <module>
    testtwo()
TypeError: 'new' object is not callable
>>> old.__call__

Traceback (most recent call last):
  File "<pyshell#31>", line 1, in <module>
    old.__call__
AttributeError: class old has no attribute '__call__'
>>> new.__call__
<method-wrapper '__call__' of type object at 0x00CC40D8>
>>> testone.__call__
<function <lambda> at 0x00C35EB0>
>>> testtwo.__call__
<function <lambda> at 0x00C35B70>
>>> dir(testtwo)
['__call__', '__class__', '__delattr__', '__dict__', '__doc__',
'__getattribute__', '__hash__', '__init__', '__module__', '__new__',
'__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__str__',
'__weakref__']
>>> dir(testone)
['__call__', '__doc__', '__module__']
>>> dir(new)
['__class__', '__delattr__', '__dict__', '__doc__',
'__getattribute__', '__hash__', '__init__', '__module__', '__new__',
'__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__str__',
'__weakref__']
>>> dir(old)
['__doc__', '__module__']

I don't see __call__ in either class structures, but for new style
classes it is a wrapper and for old it is nothing. Not sure if that
helps, but this is rather over my head.



More information about the Python-list mailing list