1. 程式人生 > >python關於類的call方法

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