如何区分调用的是函数还是方法
- 方法
class myClass:
def process(self):
pass
- 函数
def process():
判断是否是函数或者方法
- 打印type
print(type(myClass().process))
print(type(process))

- 判断name
print(type(myClass().process).__name__ == 'method')
print(type(process).__name__ == 'function')

- 使用isinstance
from types import *
print('myClass.process:',isinstance(myClass().process,MethodType))
print('process:',isinstance(process,FunctionType))

总结
1.使用type
2.使用name
3.使用isinstance,判断是functiontype还是methodtype
加油 2020-3-9
网友评论