[Python-Dev] how to inspect if something includes a bound first param

Larry Hastings larry at hastings.org
Wed Feb 25 15:33:59 CET 2015



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/
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-dev/attachments/20150225/da5c1f4d/attachment.html>


More information about the Python-Dev mailing list