changing __call__ on demand
Michael Hoffman
cam.ac.uk at mh391.invalid
Mon Feb 14 05:43:09 EST 2005
Stefan Behnel wrote:
>
> Thanks for the quick answer. I didn't know they were class-level
> methods. Too bad. Guess I'll stick with indirection then.
Here is one way of doing that indirection I just thought of--have
the class __call__ attribute call on the instance __call__
attribute:
>>> class MyClass(object):
... def __init__(self, func):
... self.__call__ = func
... def __call__(self, *args, **keywds):
... return self.__call__(*args, **keywds)
...
>>> def f1(): return "foo"
...
>>> def f2(x, y): return x+y
...
>>> MyClass(f1)()
'foo'
>>> MyClass(f2)(30, 12)
42
I still can't figure out whether this is elegant, or opaque and
to be avoided. <wink>
--
Michael Hoffman
More information about the Python-list
mailing list