Python 使用@staticmethod有什么好处吗?
我想知道您是否在代码中使用@staticmethoddecorator 我个人不使用它,因为写@staticmethod比self需要更多的字母 使用它的唯一好处(对我来说)可能是代码更清晰,但因为我通常为sphinx编写方法描述,所以我总是声明方法是否使用objectPython 使用@staticmethod有什么好处吗?,python,Python,我想知道您是否在代码中使用@staticmethoddecorator 我个人不使用它,因为写@staticmethod比self需要更多的字母 使用它的唯一好处(对我来说)可能是代码更清晰,但因为我通常为sphinx编写方法描述,所以我总是声明方法是否使用object 或者我应该开始使用@staticmethod装饰器?是否使用@staticmethod取决于您想要实现什么。忽略decorator是一个相当愚蠢的原因(无意冒犯!),它表明您还没有理解Python中静态方法的概念 静态方法独立于
或者我应该开始使用@staticmethod装饰器?是否使用
@staticmethod
取决于您想要实现什么。忽略decorator是一个相当愚蠢的原因(无意冒犯!),它表明您还没有理解Python中静态方法的概念
静态方法独立于类和任何类实例。它们仅将类作用域用作命名空间。如果省略了@staticmethod
装饰器,则创建的实例方法在不构造实例的情况下无法使用
下面是一个非常简单的类Foo
:
>>> class Foo(object):
... @staticmethod
... def foo():
... print 'foo'
...
... def bar(self):
... print 'bar'
>>> Foo.bar()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: unbound method foo() must be called with Foo instance as first argument (got nothing instead)
>>> foo = Foo()
>>> foo.bar()
bar
现在,Foo.Foo()
是一个可以直接调用的静态方法:
>>> Foo.foo()
foo
另一方面,Foo.bar()
是一种实例方法,只能从Foo
的实例(对象)调用:
>>> class Foo(object):
... @staticmethod
... def foo():
... print 'foo'
...
... def bar(self):
... print 'bar'
>>> Foo.bar()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: unbound method foo() must be called with Foo instance as first argument (got nothing instead)
>>> foo = Foo()
>>> foo.bar()
bar
>>Foo.bar()
回溯(最近一次呼叫最后一次):
文件“”,第1行,在
TypeError:必须使用foo实例作为第一个参数调用未绑定的方法foo()(但没有得到任何结果)
>>>foo=foo()
>>>foo.bar()
酒吧
回答您的问题:如果要定义静态方法,请使用@staticmethod
。否则,不要
如果您有一个方法不使用
self
,因此可以作为静态方法编写,请扪心自问:您是否希望在没有实例的情况下从外部访问此函数?大多数情况下,答案都是:不。staticmethod@decorator可以节省键入时间并提高可读性
class Example:
@staticmethod
def some_method():
return
同:
class Example:
def some_method():
return
some_method = staticmethod(some_method)
我认为您可能会对Python中的静态方法感到困惑,因为术语不同于其他语言。常规方法“绑定”到实例(self
),类方法“绑定”到类(cls
),而静态方法根本不“绑定”(并且不能访问实例或类属性)
见:
假设我们想在类数学中定义abs
方法,那么我们有两个选择:
class Math():
def abs(n):
if n>0:
return n
else:
return -n
class Math2():
@staticmethod
def abs(n):
if n>0:
return n
else:
return -n
在Python2中:
>>> Math.abs(-2)
TypeError: unbound method abs() must be called with Math instance as
first argument (got int instance instead)
>>>Math().abs(-2)
TypeError: abs() takes exactly 1 argument (2 given)
>>> Math2.abs(-2)
2
>>> Math2().abs(-2)
2
python2自动将Math().abs(-2)
作为Math().abs(self,-2)
,因此您必须使用@staticmethod
在Python3中
>>>Math.abs(-3)
3
>>>Math().abs(-3)
TypeError: abs() takes 1 positional argument but 2 were given
>>>Math2.abs(-3)
3
>>>Math2().abs(-3)
3
在python3中,您可以在不使用静态方法的情况下使用classname.method()
,但是当有人尝试使用instance.method()
时,除了前面的答案之外,还会引发TypeError,
从pythons doc@staticmethod:
它可以在类(如C.f())或实例(如C().f())上调用。除了它的类之外,实例将被忽略
+1:“忽略修饰符是一个相当愚蠢的原因,因为有更多的内容要输入”。请不要道歉。这很愚蠢。只需添加一个使用staticmethod而不是self的其他原因——这不仅适用于python,也适用于未来的维护人员。它强调“此函数不依赖于类或实例的状态”,而不是classmethod或未修饰的方法。对于不熟悉代码的维护人员(例如:你,5年后),这会使学习代码库更快。感谢你的回答,现在当我看到这真是一个愚蠢的原因:)我还有很多东西要学,但多亏了你们,学习不再像以前那么痛苦了。事实上,我仍然很困惑:在python3中有哪些地方我们必须使用staticmethod?我发现很多地方不需要使用staticmethod。python2和python3之间的差异让我很困惑。