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

 

相关文章:

  • 2022-12-23
  • 2022-12-23
  • 2021-09-22
  • 2022-12-23
  • 2022-12-23
  • 2021-12-09
  • 2022-12-23
  • 2021-12-31
猜你喜欢
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-08-12
  • 2022-12-23
相关资源
相似解决方案