__在类内调用方法时调用

2024-09-28 05:26:41 发布

您现在位置:Python中文网/ 问答频道 /正文

我想理解__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.test()将启动实例x(),它应该调用__call__方法当然。但是根据我的输出,__call__只在初始化时调用x()。在

有人能解释一下吗。在


Tags: 方法testselfbinhereinitusrdef

热门问题