python关于类的call方法

1.__ call__

object.call(self[, args…])
Called when the instance is “called” as a function; if this method is defined, x(arg1, arg2, …) is a shorthand for x.call(arg1, arg2, …).

当对象像函数一样被调用时,就相当于执行它的__ call__方法.

class Test:
    def __call__(self, *args, **kwargs):
        print('shot')

t1 = Test()
t1()

#result
shot

猜你喜欢

转载自blog.csdn.net/Mr_Slower/article/details/83589286
今日推荐