3

关于为什么/何时应该使用类方法与静态方法,有几个很好的解释,但是我无法找到何时使用静态方法而不是完全没有装饰的答案。考虑这个

class Foo(object):        
    @staticmethod
    def f_static(x):
        print("static version of f, x={0}".format(x))

    def f_standalone(x):
        print("standalone verion of f, x={0}".format(x))

还有一些输出:

>>> F = Foo
>>> f = F()
>>> F.f_static(5)
static version of f, x=5
>>> F.f_standalone(5)
standalone verion of f, x=5
>>> f.f_static(5)
static version of f, x=5
>>> f.f_standalone(5)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: f_standalone() takes 1 positional argument but 2 were given

从我在这里读到的内容来看,使用 staticmethod 的主要原因基本上是将概念上相似的东西放在一起。从上面的示例中,似乎两种解决方案都可以做到这一点。唯一的缺点是您似乎无法从实例调用非静态方法。也许我太习惯其他编程语言了,但这并没有让我很困扰;我可以从 Python 中的实例中调用类级别的东西总是令人惊讶。

那么,这基本上是两者之间的唯一区别吗?还是我错过了其他好处?谢谢

4

1 回答 1

2

您似乎正在使用 python 3。在 python 2 中:

In [1]: class Foo(object):
   ...:     def f_standalone(x):
   ...:         print("standalone version of f, x={}".format(x))
   ...:

In [2]: Foo.f_standalone(12)
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-2-2d315c006153> in <module>()
----> 1 Foo.f_standalone(12)

TypeError: unbound method f_standalone() must be called with Foo instance as first argument (got int instance instead)

在 python 3 中,您错过了另一个奇怪的用例:

In [1]: class Foo(object):
   ...:     def f_standalone(x):
   ...:         print("standalone version of f, x={}".format(x))
   ...:     @staticmethod
   ...:     def f_static(x):
   ...:         print("static version of f, x={}".format(x))
   ...:

In [2]: Foo().f_standalone()
standalone version of f, x=<__main__.Foo object at 0x1064daa10>

In [3]: Foo().f_static()
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-3-addf9c3f2477> in <module>()
----> 1 Foo().f_static()

TypeError: f_static() missing 1 required positional argument: 'x'
于 2013-03-10T00:28:05.057 回答