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))

 

posted @ 2018-12-07 23:01  RobotsRising  阅读(2472)  评论(0编辑  收藏  举报