Why can't I pass self as a named argument to an instance method in Python?
Posted
by Joseph Garvin
on Stack Overflow
See other posts from Stack Overflow
or by Joseph Garvin
Published on 2010-03-14T20:38:01Z
Indexed on
2010/03/14
20:45 UTC
Read the original article
Hit count: 279
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.
© Stack Overflow or respective owner