[Python进阶] Python定义类方法
Python中的几种定义类方法:常规方式,@property修饰方式,@classmethod修饰方式,@staticmethod修饰方式。 class Test(object): def __init__(self): self.name = 'eappo' def fun(self, x): print('self:', self, x) @property def property_fun(self): print('@property', self, self.name) @classmethod def class_fun(cls, x): print('@classmethod cls:', cls, x) @staticmethod def static_fun(x): print('@staticmethod', x) if __name__ == '__main__': a = Test() a.fun('fun') a.property_fun a.class_fun('class_fun') a.static_fun('static_fun') 运行结果: self: <__main__.Test object at 0x0000023DC8BAD208> fun @property <__main__.Test object at