You need the decorator if you intend to try to call the @staticmethod
from the instance of the class instead of of the class directly
class Foo():
def bar(x):
return x + 5
>>> f = Foo()
>>> f.bar(4)
Traceback (most recent call last):
File "<pyshell#7>", line 1, in <module>
f.bar(4)
TypeError: bar() takes 1 positional argument but 2 were given
Now if I declare @staticmethod
the self
argument isn't passed implicitly as the first argument
class Foo():
@staticmethod
def bar(x):
return x + 5
>>> f = Foo()
>>> f.bar(4)
9
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…