这有效:
>>> def bar(x, y):
... print x, y
...
>>> bar(y=3, x=1)
1 3
这有效:
>>> class Foo(object):
... def bar(self, x, y):
... print x, y
...
>>> z = Foo()
>>> z.bar(y=3, x=1)
1 3
即使这样也有效:
>>> Foo.bar(z, y=3, x=1)
1 3
但为什么这在 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)
这使得元编程变得更加困难,因为它需要特殊情况处理。我很好奇它是否是 Python 语义所必需的,或者只是实现的一个产物。