call方法

def test1():
    print('Hello test1')

# 函数为可调用对象
print(callable(test1))  # True


class test2():
    def __init__(self):
        pass
    

a = test2()
print(callable(a))  # false,实例对象是不可调用对象


class test3():
    def __init__(self):
        pass

    def __call__(self, *args, **kwargs):
        '''当test3的实例化对象被调用时会执行'''
        print('hello world')

a1 = test3()
print(callable(a1))   # true
a1()  # hello world

猜你喜欢

转载自www.cnblogs.com/fqh202/p/9383525.html