@staticmethord

python中类方法、静态方法和普通方法

ぃ、小莉子 提交于 2020-03-01 13:03:41
@classmethod vs @staticmethod vs "plain" methods What's the difference? class MyClass: def method(self): """ Instance methods need a class instance and can access the instance through `self`. """ return 'instance method called', self @classmethod def classmethod(cls): """ Class methods don't need a class instance. They can't access the instance (self) but they have access to the class itself via `cls`. """ return 'class method called', cls [@staticmethod](https://my.oschina.net/staticmethod) def staticmethod(): """ Static methods don't have access to `cls` or `self`. They work like regular