Python内置函数之staticmethod()

staticmethod(function)
返回函数的静态方法。一般来说,实例对象调用类方法不用传入参数,因为实例对象本身隐式的作为第一个参数传入了。
而采用静态方法之后,实例对象在调用类方法时必须传入一个参数了。

常被用来作为函数的装饰器。

例子:

>>> class A:
...   def f(ln):
...     print(ln)
...
>>> a = A()
>>> a.f()
<__main__.A object at 0x000000D30C6E9DD8>
>>> class A:
...   @staticmethod
...   def f(ln):
...     print(ln)
...
>>> a = A()
>>> a.f()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: f() missing 1 required positional argument: 'ln'
>>> a.f('hi')
hi
原文地址:https://www.cnblogs.com/leomei91/p/7358904.html