Mọi Python class có 3 loại method: instance method, class method, static method.
Ví dụ
class OurClass:
def method(self):
return 'instance method called', self
@classmethod
def classmethod(cls):
return 'class method called', cls
@staticmethod
def staticmethod():
return 'static method called'
Method thứ ba OurClass.staticmethod được đánh dấu @staticmethod decorator flag là static method.
Loại method này không nhận self cũng không nhận cls nhưng có thể nhận số lượng tham số khác tùy ý.
Do đó static method không thể modify object state cũng không modify class state. Static method bị hạn chế data truy cập - chủ yếu là cách namespace các method. Gọi staticmethod:
>>> obj = OurClass() >>> obj.staticmethod() 'static method called'