• @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()

 

相关文章:

  • 2022-12-23
  • 2021-09-05
  • 2021-08-14
  • 2021-12-18
  • 2021-12-27
  • 2022-12-23
  • 2021-11-11
  • 2021-10-27
猜你喜欢
  • 2021-11-29
  • 2021-08-28
  • 2022-12-23
  • 2021-08-17
  • 2022-02-07
  • 2021-05-28
  • 2022-12-23
相关资源
相似解决方案