Lawrence Oluyede a écrit : > Bruno Desthuilliers <[EMAIL PROTECTED]> > 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) -- http://mail.python.org/mailman/listinfo/python-list