[Python-Dev] Dinamically set __call__ method
Ethan Furman
ethan at stoneleaf.us
Sat Nov 8 22:27:41 EST 2014
On 11/08/2014 02:31 PM, Gregory Ewing wrote:
>
> Seems to depend on how you get hold of the object you're
> inspecting the signature of. I did an experiment:
>
> class C(object):
>
> @property
> def __call__(self):
> return self.call
>
> def f(x, y):
> print("Called f with %s, %s" % (x, y))
>
> c = C()
> c.call = f
> c(17, 42)
>
> prints:
> Called f with 17, 42
>
> This works too:
>
> import inspect
> print(inspect.getargspec(c.__call__))
>
> prints:
> ArgSpec(args=['x', 'y'], varargs=None, keywords=None, defaults=None)
>
> But
>
> print(inspect.getargspec(c))
>
> doesn't work:
>
> TypeError: <__main__.C object at 0x479250> is not a Python function
>
> (BTW, I'm actually surprised that this technique makes c callable.
> There must be more going on that just "look up __call__ in the class
> object", because evaluating C.__call__ just returns the descriptor
> and doesn't invoking the descriptor mechanism.)
Looks like you found a bug in inspect.getargspec.
And the thing going on is the normal python behavior (in __getattribute__, I believe) of examining the returned
attribute to see if it is a descriptor, and if so invoking it.
--
~Ethan~
More information about the Python-list
mailing list