判断python对象是否可调用的三种方式及其区别详解
查找资料,基本上判断python对象是否为可调用的函数,有三种方法
使用内置的callable函数
callable(func)
用于检查对象是否可调用,返回True也可能调用失败,但是返回False一定不可调用。
官方文档:https://docs.python.org/3/library/functions.html?highlight=callable#callable
判断对象类型是否是FunctionType
type(func)isFunctionType #或者 isinstance(func,FunctionType)
判断对象是否实现__call__方法
hasattr(func,'__call__')
写个小demo,测试下这三种验证方式的区别
fromtypesimportFunctionType __author__='blackmatrix' classClassA: @staticmethod deffunc_a(): pass @classmethod deffunc_b(cls,arg): pass deffunc_c(self,arg): pass deffunc_d(): pass if__name__=='__main__': class_a=ClassA() print('静态方法,实例调用验证') print("callable(class_a.func_a)result:{result}".format(result=callable(class_a.func_a))) print("type(class_a.func_a)isFunctionTyperesult:{result}".format(result=type(class_a.func_a)isFunctionType)) print("hasattr(class_a.func_a,'__call__')result:{result}".format(result=hasattr(class_a.func_a,'__call__'))) print('静态方法,类调用验证') print("callable(ClassA.func_a)result:{result}".format(result=callable(ClassA.func_a))) print("type(ClassA.func_a)isFunctionTyperesult:{result}".format(result=type(ClassA.func_a)isFunctionType)) print("hasattr(ClassA.func_a,'__call__')result:{result}".format(result=hasattr(ClassA.func_a,'__call__'))) print('类方法验证') print("callable(ClassA.func_b)result:{result}".format(result=callable(ClassA.func_b))) print("type(ClassA.func_b)isFunctionTyperesult:{result}".format(result=type(ClassA.func_b)isFunctionType)) print("hasattr(ClassA.func_b,'__call__')result:{result}".format(result=hasattr(ClassA.func_b,'__call__'))) print('实例方法验证') print("callable(class_a.func_c)result:{result}".format(result=callable(class_a.func_c))) print("type(class_a.func_c)isFunctionTyperesult:{result}".format(result=type(class_a.func_c)isFunctionType)) print("hasattr(class_a.func_c,'__call__')result:{result}".format(result=hasattr(class_a.func_c,'__call__'))) print('函数验证') print("callable(func_d)result:{result}".format(result=callable(func_d))) print("type(func_d)isFunctionTyperesult:{result}".format(result=type(func_d)isFunctionType)) print("hasattr(func_d,'__call__')result:{result}".format(result=hasattr(func_d,'__call__')))
通过运行结果,发现三种方法的验证结果并不相同。
主要是type(func)isFunctionType方法,在验证类方法和实例方法时,会返回False,
从调试的结果上看,实例方法,和类方法的类型都是
静态方法,实例调用验证 callable(class_a.func_a)result:True type(class_a.func_a)isFunctionTyperesult:True hasattr(class_a.func_a,'__call__')result:True 静态方法,类调用验证 callable(ClassA.func_a)result:True type(ClassA.func_a)isFunctionTyperesult:True hasattr(ClassA.func_a,'__call__')result:True 类方法验证 callable(ClassA.func_b)result:True type(ClassA.func_b)isFunctionTyperesult:False hasattr(ClassA.func_b,'__call__')result:True 实例方法验证 callable(class_a.func_c)result:True type(class_a.func_c)isFunctionTyperesult:False hasattr(class_a.func_c,'__call__')result:True 函数验证 callable(func_d)result:True type(func_d)isFunctionTyperesult:True hasattr(func_d,'__call__')result:True
因为Python中分为函数(function)和方法(method),函数是Python中一个可调用对象(用户定义的可调用对象,及lambda表达式创建的函数,都是函数,其类型都是FunctionType),方法是一种特殊的类函数。
官方文档中,对于method的定义:
Methodsarealwaysboundtoaninstanceofauser-definedclass
类方法和类进行绑定,实例方法与实例进行绑定,所以两者的类型都是method。
而静态方法,本身即不和类绑定,也不和实例绑定,不符合上述定义,所以其类型应该是function。
其中还有需要注意的是,如果一个类实现了__call__方法,那么其实例也会成为一个可调用对象,其类型为创建这个实例的类,而不是函数或方法。
classTheClass: def__call__(self,*args,**kwargs): returnself if__name__=='__main__': the_class=TheClass() #True print('class_instancecallable{callable}'.format(callable=callable(the_class)))
所以通过类型去判断Python对象是否可调用,需要同时判断是函数(FunctionType)还是方法(MethodType),或者类是否实现__call__方法。
如果只是单纯判断python对象是否可调用,用callable()方法会更稳妥。
以上这篇判断python对象是否可调用的三种方式及其区别详解就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持毛票票。