这有效:
>>> def bar(x, y):
... print x, y
...
>>> bar(y=3, x=1)
1 3
Run Code Online (Sandbox Code Playgroud)
这有效:
>>> class Foo(object):
... def bar(self, x, y):
... print x, y
...
>>> z = Foo()
>>> z.bar(y=3, x=1)
1 3
Run Code Online (Sandbox Code Playgroud)
甚至这个工作:
>>> Foo.bar(z, y=3, x=1)
1 3
Run Code Online (Sandbox Code Playgroud)
但为什么这不起作用?
>>> 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)
Run Code Online (Sandbox Code Playgroud)
这使得元编程更加困难,因为它需要特殊的案例处理.我很好奇Python的语义是否是必要的,或者仅仅是实现的工件.