staticmethod 允许你用实例来调用。。。
相当于自动忽略了那个 self 参数
>>> class Test:
... def nonstatic(a, b):
... print(a, b)
... @staticmethod
... def static(a, b):
... print(a, b)
...
>>> Test.nonstatic(1, 2)
1 2
>>> Test.static(1, 2)
1 2
>>> t = Test()
>>> t.nonstatic(1, 2)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: nonstatic() takes 2 positional arguments but 3 were given
>>> t.static(1, 2)
1 2
【 在 JulyClyde (我的月份又来了) 的大作中提到: 】
: staticmethod decorator是什么内容呢?
: 既然不加也行,那似乎也没干啥正经事?
--
FROM 114.242.94.*