函数方法区别

Posted 百连

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了函数方法区别相关的知识,希望对你有一定的参考价值。

- 函数和方法?

class Foo(object):
def __init__(self):
self.name = ‘alex‘
def func(self):
print(self.name)

from types import FunctionType,MethodType

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

  


注意:
方法,无需传入self参数
函数,必须手动传入self参数

以上是关于函数方法区别的主要内容,如果未能解决你的问题,请参考以下文章