changing __call__ on demand
Kent Johnson
kent37 at tds.net
Sun Feb 13 14:26:52 EST 2005
Stefan Behnel wrote:
> Hi!
>
> This somewhat puzzles me:
>
> Python 2.4 (#1, Feb 3 2005, 16:47:05)
> [GCC 3.3.4 (pre 3.3.5 20040809)] on linux2
> Type "help", "copyright", "credits" or "license" for more information.
>
> .>>> class test(object):
> ... def __init__(self):
> ... self.__call__ = self.__call1
> ... def __call1(self):
> ... print 1
> ... def __call__(self):
> ... print 2
> ...
> .>>> t = test()
> .>>> t()
> 2
>
It works the way you want if test is an old-style class:
>>> class test:
... def __init__(self):
... self.__call__ = self.__call1
... def __call1(self):
... print 1
... def __call__(self):
... print 2
...
>>> t=test()
>>> t()
1
Kent
More information about the Python-list
mailing list