【发布时间】:2014-05-29 05:12:39
【问题描述】:
请参阅此示例进行演示:
>>> class M:
def __init__(self): self.x = 4
>>> sample = M()
>>> def test(self):
print(self.x)
>>> sample.test = test
>>> sample.test()
Traceback (most recent call last):
File "<pyshell#17>", line 1, in <module>
sample.test()
TypeError: test() missing 1 required positional argument: 'self'
为什么?
【问题讨论】:
标签: python oop python-3.x self monkeypatching