@staticmethod和@classmethod

  • @staticmethod:不需要表示自身对象的self和自身类的cls参数,就跟使用函数一样。
  • @classmethod也不需要self参数,但第一个参数需要是表示自身类的cls参数。
class MyClass:
    def method(self):
        return 'instance method called', self

    @classmethod
    def classmethod(cls):
        return 'class method called', cls

    @staticmethod
    def staticmethod():
        return 'static method called'
a = MyClass()

 

posted @ 2019-07-15 17:01  anobscureretreat  阅读(123)  评论(0)    收藏  举报