【发布时间】:2016-05-21 04:35:02
【问题描述】:
我注意到,如果我在创建该类的实例时定义了与函数相等的类属性,则该属性将成为绑定方法。有人可以解释一下这种行为的原因吗?
In [9]: def func():
...: pass
...:
In [10]: class A(object):
....: f = func
....:
In [11]: a = A()
In [12]: a.f
Out[12]: <bound method A.func of <__main__.A object at 0x104add190>>
In [13]: a.f()
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-13-19134f1ad9a8> in <module>()
----> 1 a.f()
global a.f = <bound method A.func of <__main__.A object at 0x104add190>>
TypeError: func() takes no arguments (1 given)
【问题讨论】:
标签: python methods class-attributes