Dynamic method
Bruno Desthuilliers
bruno.42.desthuilliers at wtf.websiteburo.oops.com
Mon Jul 16 10:29:16 EDT 2007
Lawrence Oluyede a écrit :
> Bruno Desthuilliers <bruno.42.desthuilliers at wtf.websiteburo.oops.com>
> wrote:
>>> I agree that in general the solution explained by Alex and you is better.
>> They are not "better" - they are correct.
>
> Another way is to use the 'types' module:
True - and that's somewhat cleaner since it doesn't expose the internals
of the descriptor protocol. OTHO, it can lead to strange results with
callables not implementing the descriptor protocol:
class MyCallable(object):
def __init__(self, name):
self.name = name
def __call__(self):
print self.name
fun = MyCallable('gizmo')
class Foo(object):
pass
f = Foo()
f.fun = types.MethodType(fun, f, Foo)
f.fun()
=>
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/tmp/python-17437zds.py", line 16, in <module>
f.fun()
TypeError: __call__() takes exactly 1 argument (2 given)
More information about the Python-list
mailing list