Why can't I pass myself as a named argument to an instance method in Python?

It works:

>>> def bar(x, y):
...     print x, y
...
>>> bar(y=3, x=1)
1 3

And it 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 does this not work in Python 2.x?

>>> 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 complicates metaprogramming because it requires special processing. I'm curious if this is somehow necessary on the semantics of Python or just an implementation artifact.

+5
source share
1 answer

z.bar - - im_self, ( self) , im_func. , , , im_self ( : im_func) - , , , . , Python ( : Python ). "" , , Python , . , , , - , , Python.

: OP , , . , :

TypeError: unbound method bar() Foo ( )

, , , (, , : ). "" ( ) , ( self - , Python): " " ( , ).

, , ( Python 3.2, "" ;-), : - , - self . , , Python. , , python-dev, , , , > 0 - .

im_func , - , , - " " , , , ( " ", ( , , : , - - ! -).

+6

All Articles