python中的__call__用于使对象具有可执行性,举例如下
class Example: def __call__(self): print("this is a class call") def func(self): print("this is a func call") example = Example() # create an instance example() # execute call function example.func() # execute func
在深度学习框架中存在着大量类似的应用,如pytorch的Module对__call__进行了实现,从而实现了继承于Module的类具备可执行性,即默认调用forward方法并返回
class Module: def __call__(self, *input, **kwargs): ### result = self.forward(*input, **kwargs) return result