码迷,mamicode.com
首页 > 其他好文 > 详细

@staticmethod和@classmethod

时间:2019-07-15 17:17:06      阅读:89      评论:0      收藏:0      [点我收藏+]

标签:使用   nbsp   turn   call   函数   参数   pre   span   第一个   

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

 

@staticmethod和@classmethod

标签:使用   nbsp   turn   call   函数   参数   pre   span   第一个   

原文地址:https://www.cnblogs.com/sea-stream/p/11189985.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!