__call__ question
Michael Hudson
mwh at python.net
Tue Oct 1 07:31:25 EDT 2002
Rick <r.l.richardson at verizon.net> writes:
> If I generate a call method inside a class class with def
> __call__(self, arg1): ...
> everything is hunkey dorey. However if I generate the class then
> later try to add the __call__ function with setattribute or by create
> a function that behaves like call should and then assigning it to
> myobj.__class__ I can't call the instance of the class like a callable
> object. It states that that instance is not a callable object. I can
> however:
>
> myobj.__call__(myobj, arg1)
> and it works. Anyone know what I have to do to make an object callable
> at runtime?
Works for me:
>>> class C:
... pass
...
>>> c = C()
>>>
>>> def call(self, x):
... print self, x
...
>>> C.__call__ = call
>>>
>>> c(2)
<__main__.C instance at 0x81929e4> 2
Works the same with new-style classes too. What goes wrong for you?
Cheers,
M.
--
I'd certainly be shocked to discover a consensus. ;-)
-- Aahz Maruch, comp.lang.python
More information about the Python-list
mailing list