一、通过print打印区分
- 函数在打印的时候,显示的是function
- 方法在打印的时候,显示的是method
def func():
pass
class Animal(object):
def run(self):
pass
print(func) # <function func at 0x0000022343CD1F28>
c = Animal()
print(c.run) # <bound method Animal.run of <__main__.Animal object at 0x0000022344128400>>
二、MethodType与FunctionType
1. 实例方法
- 通过对象.方法名,得到的是方法类型
- 通过类名.方法名,得到的是函数类型
from types import FunctionType, MethodType
class Foo(object):
def run(self):
pass
fn = Foo()
print(isinstance(fn.run, MethodType)) # True
print(isinstance(Foo.run, FunctionType)) # True
2. 静态方法
- 通过对象.方法名,得到的是函数类型
- 通过类名.方法名,得到的是函数类型
from types import FunctionType, MethodType
class Foo(object):
@staticmethod
def run():
pass
fn = Foo()
print(isinstance(fn.run, FunctionType)) # True
print(isinstance(Foo.run, FunctionType)) # True
3. 类方法
- 通过对象.方法名,得到的是方法类型
- 通过类名.方法名,得到的是方法类型
from types import FunctionType, MethodType
class Foo(object):
@classmethod
def run(cls):
pass
fn = Foo()
print(isinstance(fn.run, MethodType)) # True
print(isinstance(Foo.run, MethodType)) # True