Skip to main content
 首页 » 编程设计

python之python 中的猴子修补绑定(bind)方法

2025年05月04日29me-sa

<分区>

>>> class A: 
...     def foo(self): 
...             print(self) 
... 
>>> 
>>> a = A() 
>>> a.foo() 
<__main__.A instance at 0x7f4399136cb0> 
>>> def foo(self): 
...     print(self) 
... 
>>> a.foo = foo 
>>> a.foo() 
Traceback (most recent call last): 
  File "<stdin>", line 1, in <module> 
TypeError: foo() takes exactly 1 argument (0 given) 

我想了解 Python 中的猴子修补。请说明错误原因和解决方法。