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

Steven D'Aprano steve at REMOVE-THIS-cybersource.com.au
Thu Jul 10 02:55:45 EDT 2008


On Tue, 08 Jul 2008 17:56:45 -0400, Joseph Barillari wrote:

> Hi python-list,
> 
> I've just started using new-style classes and am a bit confused as to
> why I can't seem to alter methods with special names (__call__, etc.) of
> new-style class instances.

[deploy weapon of mass snippage]

Here is a possible work-around:

>>> class Special(object):
...     def __call__(self):
...         try:
...             return self.__dict__['__call__']()
...         except KeyError:
...             return 'foo'
...
>>> s = Special()
>>> s()
'foo'
>>> s.__call__ = lambda: 'bar'
>>> s()
'bar'



-- 
Steven



More information about the Python-list mailing list