Keyword for first argument of methods?
Hi, When implementing keyword argument support for Boost.Python, I noticed the following. I'm sure it's not worth a lot of effort to change this behavior, but I thought someone might like to know:
class X: ... def foo(self, y): print y ... X.foo(y = 1, self = X()) Traceback (most recent call last): File "<stdin>", line 1, in ? TypeError: unbound method foo() must be called with X instance as first argument (got nothing instead)
-Dave ----------------------------------------------------------- David Abrahams * Boost Consulting dave@boost-consulting.com * http://www.boost-consulting.com
Dave> When implementing keyword argument support for Boost.Python, I noticed the Dave> following. I'm sure it's not worth a lot of effort to change this behavior, Dave> but I thought someone might like to know: Dave> class X: Dave> ... def foo(self, y): print y Dave> ... Dave> X.foo(y = 1, self = X()) Dave> Traceback (most recent call last): Dave> File "<stdin>", line 1, in ? Dave> TypeError: unbound method foo() must be called with X instance as first Dave> argument (got nothing instead) Perhaps more interesting: >>> X.foo(X(), 1) 1 >>> X.foo(self = X(), y = 1) TypeError: unbound method foo() must be called with X instance as first argument (got nothing instead) -- Andrew Koenig, ark@research.att.com, http://www.research.att.com/info/ark
From: "Andrew Koenig" <ark@research.att.com>
Dave> When implementing keyword argument support for Boost.Python, I noticed the Dave> following. I'm sure it's not worth a lot of effort to change this behavior, Dave> but I thought someone might like to know:
Dave> class X: Dave> ... def foo(self, y): print y Dave> ... Dave> X.foo(y = 1, self = X()) Dave> Traceback (most recent call last): Dave> File "<stdin>", line 1, in ? Dave> TypeError: unbound method foo() must be called with X instance as first Dave> argument (got nothing instead)
Perhaps more interesting:
>>> X.foo(X(), 1) 1 >>> X.foo(self = X(), y = 1) TypeError: unbound method foo() must be called with X instance as first argument (got nothing instead)
Given my post, that behavior falls out of the (nicely documented) rules for how functions are called, so it's unsurprising if you read the docs. I wonder if that makes any difference in the real world ;-) ----------------------------------------------------------- David Abrahams * Boost Consulting dave@boost-consulting.com * http://www.boost-consulting.com
Dave> Given my post, that behavior falls out of the (nicely Dave> documented) rules for how functions are called, so it's Dave> unsurprising if you read the docs. I wonder if that makes any Dave> difference in the real world ;-) Probably not.
When implementing keyword argument support for Boost.Python, I noticed the following. I'm sure it's not worth a lot of effort to change this behavior, but I thought someone might like to know:
class X: ... def foo(self, y): print y ... X.foo(y = 1, self = X()) Traceback (most recent call last): File "<stdin>", line 1, in ? TypeError: unbound method foo() must be called with X instance as first argument (got nothing instead)
You can't pass in self to an unbound method as a keyword argument -- it has to be the first positional argument. The unbound method __call__ implementation contains a check that ensures that the 'self' argument is an instance of the class, but when this check is made, it cannot assume that the 'self' argument is actually called 'self' -- that's only a naming convention. It also doesn't know (in general) the name of the first argument to the underlying function, since the function can be an arbitrary callable -- there's no standard introspection interface for callables to find out the argument names. As you said, I see no reason to try to work harder in the case that the underlying callable supports introspection using obj.func_code.co_{argcount,varnames}. --Guido van Rossum (home page: http://www.python.org/~guido/)
participants (3)
-
Andrew Koenig
-
David Abrahams
-
Guido van Rossum