Python 中方法和函数的区别

类实例化出来去调用,叫方法

直接使用类名去调用,叫函数

from types import FunctionType, MethodType


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
False
True
True
False
原文地址:https://www.cnblogs.com/xioawu-blog/p/12484752.html