Python判断函数与方法

一、使用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))
相关文章
相关标签/搜索