Python的OOP概念中有三种方法——实例方法、类方法和静态方法。
class MyClass:
def instancemethod(self):
return 'instance method called'
@classmethod
def classmethod(cls):
return 'class method called'
@staticmethod
def staticmethod():
return 'static method called'
很明显知道所有三种方法。现在我在一个类中创建一个新方法:
class Test():
def ppr():
print('what method is ppr?')
它不是实例方法。
inst = Test()
inst.ppr()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: ppr() takes 0 positional arguments but 1 was given
所以它是一个类方法?
Test.ppr()
what method is ppr?
没有@classmethod关键字来装饰ppr函数。