经验分享 – 52 – 判断调用的是函数还是方法

如何区分调用的是函数还是方法

class MyClass():
    def process(self):
        pass
    
def process():
    pass

print(type(MyClass().process).__name__ == 'method')
print(type(process).__name__ == 'function')

True

True

MyClass.process  False

MyClass.process  True

from types import MethodType, FunctionType
'''
函数: FunctionType
方法: MethodType
'''

print('MyClass.process ', isinstance(MyClass().process, FunctionType))
print('MyClass.process ', isinstance(MyClass().process, MethodType))

print('process ', isinstance(process, FunctionType))
print('process ', isinstance(process, MethodType))

MyClass.process  False

MyClass.process  True

process  True

process  False

正文完