__call__   方法的作用和用法

ernestwang 784 0

对象当作函数被调用: __call__   方法 class Person: # 将对象当做函数调用时会自动触发 def __call__(self, *args, **kwargs): print('__call__') print(args) print(kwargs)   p = Person() 把对象当成函数的时候自动触发。   # 判断是否可调用 # print(callable(p))   # 判断是否有call属性 print(hasattr(p, '__call__'))   # 判断是否是函数 from inspect import isfunction print(isfunction(p))   抽象基类: from abc import ABC, abstractmethod   class Animal(ABC): # 定义抽象方法:子类中必须实现,所有可以统一接口  @abstractmethod def run(self): 凡是在父类上定义了@abstracmethod的方法,子类和必须使用,否则无法创建对象。

发布评论 0条评论)

还木有评论哦,快来抢沙发吧~

复制成功
微信号: irenyuwang
关注微信公众号,站长免费提供流量增长方案。
我知道了