Why can't I pass self as a named argument to an instance method in Python?
- by Joseph Garvin
This works:
>>> def bar(x, y):
... print x, y
...
>>> bar(y=3, x=1)
1 3
And this works:
>>> class foo(object):
... def bar(self, x, y):
... print x, y
...
>>> z = foo()
>>> z.bar(y=3, x=1)
1 3
And even this works:
>>> foo.bar(z, y=3, x=1)
1 3
But why doesn't this work?
>>> foo.bar(self=z, y=3, x=1)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: unbound method bar() must be called with foo instance as first argument (got nothing instead)
This makes metaprogramming more difficult, because it requires special case handling. I'm curious if it's somehow necessary by Python's semantics or just an artifact of implementation.