實例化出來的去調用,叫做方法.
直接使用類名去調用,叫做函數.
from types import MethodType,FunctionType
class Foo(object):
def __init__(self):
self.name="haiyan"
def func(self):
print(self.name)
obj = Foo()
print(isinstance(obj.func,FunctionType)) #False
print(isinstance(obj.func,MethodType)) #True #說明這是一個方法
print(isinstance(Foo.func,FunctionType)) #True #說明這是一個函數。
print(isinstance(Foo.func,MethodType)) #False
注意,這只是在 python3 中才有的區分,python2 中全部稱爲方法。