为什么要使用staticmethod而不是装饰器

时间:2013-03-10 00:19:42

标签: python oop function static-methods

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

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

从我在这里读到的,使用static方法的主要原因基本上是将概念上相似的东西放在一起。从上面的例子来看,似乎两种解决方案都是这样做的。唯一的缺点是您似乎无法从实例调用非静态方法。也许我已经习惯了其他编程语言,但这并没有让我感到烦恼;我总是可以从Python中的实例调用类级别的东西。

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

1 个答案:

答案 0 :(得分: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'