Maybe a bit of example code will help: Notice the difference in the call signatures of foo
, class_foo
and static_foo
:
(也许有一些示例代码会有所帮助:注意foo
, class_foo
和static_foo
的调用签名之间的static_foo
:)
class A(object):
def foo(self, x):
print "executing foo(%s, %s)" % (self, x)
@classmethod
def class_foo(cls, x):
print "executing class_foo(%s, %s)" % (cls, x)
@staticmethod
def static_foo(x):
print "executing static_foo(%s)" % x
a = A()
Below is the usual way an object instance calls a method.
(以下是对象实例调用方法的常用方法。)
The object instance, a
, is implicitly passed as the first argument. (对象实例a
作为第一个参数隐式传递。)
a.foo(1)
# executing foo(<__main__.A object at 0xb7dbef0c>,1)
With classmethods , the class of the object instance is implicitly passed as the first argument instead of self
.
(使用classmethods ,对象实例的类作为第一个参数而不是self
隐式传递。)
a.class_foo(1)
# executing class_foo(<class '__main__.A'>,1)
You can also call class_foo
using the class.
(您也可以使用该类来调用class_foo
。)
In fact, if you define something to be a classmethod, it is probably because you intend to call it from the class rather than from a class instance. (实际上,如果您将某些东西定义为类方法,则可能是因为您打算从类而不是从类实例调用它。)
A.foo(1)
would have raised a TypeError, but A.class_foo(1)
works just fine: (A.foo(1)
会引发TypeError,但是A.class_foo(1)
可以正常工作:)
A.class_foo(1)
# executing class_foo(<class '__main__.A'>,1)
One use people have found for class methods is to create inheritable alternative constructors .
(人们发现类方法的一种用途是创建可继承的替代构造函数 。)
With staticmethods , neither self
(the object instance) nor cls
(the class) is implicitly passed as the first argument.
(使用staticmethods时 , self
(对象实例)和cls
(类)都不会隐式传递为第一个参数。)
They behave like plain functions except that you can call them from an instance or the class: (它们的行为类似于普通函数,不同之处在于您可以从实例或类中调用它们:)
a.static_foo(1)
# executing static_foo(1)
A.static_foo('hi')
# executing static_foo(hi)
Staticmethods are used to group functions which have some logical connection with a class to the class.
(静态方法用于对与类之间具有某种逻辑联系的函数进行分组。)
foo
is just a function, but when you call a.foo
you don't just get the function, you get a "partially applied" version of the function with the object instance a
bound as the first argument to the function.
(foo
仅仅是一个函数,但是当你调用a.foo
你不只是得到的功能,你会得到一个“部分应用”功能与对象实例的版本a
绑定作为第一个参数的函数。)
foo
expects 2 arguments, while a.foo
only expects 1 argument. (foo
需要2个参数,而a.foo
仅需要1个参数。)
a
is bound to foo
.
(a
绑定到foo
。)
That is what is meant by the term "bound" below: (这就是下面的术语“绑定”的含义:)
print(a.foo)
# <bound method A.foo of <__main__.A object at 0xb7d52f0c>>
With a.class_foo
, a
is not bound to class_foo
, rather the class A
is bound to class_foo
.
(使用a.class_foo
, a
不会绑定到class_foo
,而类A
会绑定到class_foo
。)
print(a.class_foo)
# <bound method type.class_foo of <class '__main__.A'>>
Here, with a staticmethod, even though it is a method, a.static_foo
just returns a good 'ole function with no arguments bound.
(在这里,即使使用静态方法,即使它是一种方法, a.static_foo
只会返回一个很好的'ole函数,且不带任何参数。)
static_foo
expects 1 argument, and a.static_foo
expects 1 argument too. (static_foo
需要1个参数,而a.static_foo
需要1个参数。)
print(a.static_foo)
# <function static_foo at 0xb7d479cc>
And of course the same thing happens when you call static_foo
with the class A
instead.
(当然,当您用类A
调用static_foo
时, static_foo
发生同样的事情。)
print(A.static_foo)
# <function static_foo at 0xb7d479cc>