我想理解__call__
(python3)的意思。编写本文是为了区分每个方法__init__
、__call__
和测试方法。在
#!/usr/bin/python3
class thara(object):
def __init__(self):
print("init called")
def __call__(self):
print("call called")
def test(self):
print("test called")
x=thara() ### constructor calling here
x() ## __call__ calling here
x.test() ## test method calling here
我的问题是当我启动x.test()
时,它为什么不调用{x()
,它应该调用__call__
方法当然。但是根据我的输出,__call__
只在初始化时调用x()
。在
有人能解释一下吗。在
https://docs.python.org/2/reference/datamodel.html#object.__call__
当实例像函数一样被调用时,调用“调用”。这就是你对
x()
的操作。x.test()
正在调用实例的方法,而不是实例本身。在相关问题 更多 >
编程相关推荐