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
本文详细解析了Python中__call__魔术方法的工作原理及其使用场景。通过实例演示了如何将类实例作为函数调用,揭示了x(arg1,arg2,...)实际上是调用了x.call(arg1,arg2,...)的秘密。
4238





