如果您打算尝试@staticmethod
从类的实例而不是直接从类的实例调用,则需要装饰器
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
现在,如果我宣布@staticmethod
该self
参数不隐式传递作为第一个参数
class Foo():
@staticmethod
def bar(x):
return x + 5
>>> f = Foo()
>>> f.bar(4)
9