Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
332 views
in Technique[技术] by (71.8m points)

python - 静态方法和类方法之间的区别(Difference between staticmethod and classmethod)

@staticmethod装饰的函数和用@staticmethod装饰的函数有@staticmethod @classmethod

  ask by Daryl Spitzer translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Maybe a bit of example code will help: Notice the difference in the call signatures of foo , class_foo and static_foo :

(也许有一些示例代码会有所帮助:注意fooclass_foostatic_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_fooa不会绑定到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>

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...