Python判断函数与方法


1、使用types模块的FunctionType,MethodType判断是函数还是方法

 1 def func():
 2     pass
 3 
 4 class Foo(object):
 5 
 6     def func(self):
 7         pass
 8 
 9 from types import FunctionType,MethodType
10 
11 obj = Foo()
12 # 是否是函数:False
13 print(isinstance(obj.func,FunctionType))
14 # 是否是方法:True
15 print(isinstance(obj.func,MethodType))
16 
17 # 是否是函数:True
18 print(isinstance(Foo.func,FunctionType))
19 # 是否是方法:False
20 print(isinstance(Foo.func,MethodType))

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM