python 中__init__ 与 __call__ 的区别

可以看到,
在类实例化的时候,会调用__init__
实例化了之后,再调用实例化的对象的时候,这个时候调用的是__call__

class test:
    def __init__(self, a, b):
        print(a + b)
        
    def __call__(self, a, b):
        print(a * b)

>>> a  =  test(1, 2)
>>> 3
>>> a(1, 2)
>>> 2

猜你喜欢

转载自blog.csdn.net/zdx1996/article/details/112636157