为什么要使用staticmethod而不是根本没有装饰器 [英] Why use staticmethod instead of no decorator at all

查看:47
本文介绍了为什么要使用staticmethod而不是根本没有装饰器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

SO上有一些很好的解释,说明为什么/何时应该使用类方法还是静态方法,但是我无法找到何时使用静态方法而不进行修饰的答案.考虑一下

There are several good explanations on SO about why/when you should use a class method vs a static method, but I've not been able to find an answer for when you would use a static method over no decoration at all. Consider this

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中的am实例调用类级的东西,这总是令人惊讶.

From what I've read on here, the primary reason for using staticmethod is basically to keep conceptually similar things together. From the example above, it seems like both solutions do that. The only drawback is that you don't appear to be able to call the non-staticmethod from an instance. Maybe I'm too used to other programming languages, but this does not bother me so much; it's always surprising that I can call class-level stuff from am instance in Python.

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

So, is this basically the only difference between the two? Or am I missing other benefits? Thanks

推荐答案

您似乎正在使用python3.在python 2中:

You seem to be using python 3. In 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 python 3, you missed another strange use case:

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'

这篇关于为什么要使用staticmethod而不是根本没有装饰器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆