On 02/24/2015 05:56 PM, Gregory P. Smith wrote:
inspect.getargspec(method) and inspect.signature(method) both include
the 'self' parameter but how are we to figure out from method itself
that it is actually bound and that its first parameter is expected to
be a bound instance?
Given the mechanisms involved, surely this question is a bit
nonsensical? The function doesn't "expect" anything, it's just a
function. (I remind you, Python 3 dropped the whole concept of an
"unbound method".) If it happens to live inside a class, and it's
accessed through an instance of the class, then the first parameter gets
bound.
Consider:
>>> class A:
... def x(self, a): print(a)
...
>>> a = A()
inspect.signature(A.x).parameters has two parameters, "self" and "a".
inspect.signature(a.x).parameters has only one parameter, "a".
I claiim this is what you want. It's analagous to a functools.partial
object. It would be awfully confusing if the signature of a
functools.partial object include the parameters handled by the partial
object.
IMO inspect.getargspec and inspect.getfullargspec get this wrong; for
a.x they include the "self" parameter. If you were constructing a call
to this function dynamically you'd include one too many parameters.
//arry/
_______________________________________________
Python-Dev mailing list
[email protected]
https://mail.python.org/mailman/listinfo/python-dev
Unsubscribe:
https://mail.python.org/mailman/options/python-dev/archive%40mail-archive.com