静态方法和类方法之间的区别 [英] Difference between staticmethod and classmethod

查看:221
本文介绍了静态方法和类方法之间的区别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

@staticmethod 装饰的函数和一个用 @classmethod ?

推荐答案

也许一些示例代码会有所帮助:请注意fooclass_foostatic_foo的呼叫签名的区别:

Maybe a bit of example code will help: Notice the difference in the call signatures of foo, class_foo and 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()

以下是对象实例调用方法的常用方法.对象实例a作为第一个参数隐式传递.

Below is the usual way an object instance calls a method. The object instance, a, is implicitly passed as the first argument.

a.foo(1)
# executing foo(<__main__.A object at 0xb7dbef0c>,1)


使用类方法,对象实例的类作为第一个参数而不是self隐式传递.


With classmethods, the class of the object instance is implicitly passed as the first argument instead of self.

a.class_foo(1)
# executing class_foo(<class '__main__.A'>,1)

您也可以使用该类来调用class_foo.实际上,如果您将某项定义为 一个类方法,可能是因为您打算从类而不是从类实例中调用它. A.foo(1)会引发TypeError,但是A.class_foo(1)可以正常工作:

You can also call class_foo using the class. 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.class_foo(1)
# executing class_foo(<class '__main__.A'>,1)

人们发现类方法的一种用途是创建可继承的替代构造函数.

One use people have found for class methods is to create inheritable alternative constructors.

使用静态方法self(对象实例)和cls(类)都不会隐式传递为第一个参数.它们的行为类似于普通函数,不同之处在于您可以从实例或类中调用它们:

With staticmethods, neither self (the object instance) nor cls (the class) is implicitly passed as the first argument. 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只是一个函数,但是当您调用a.foo时,您不仅会获得该函数, 您将获得该函数的部分应用"版本,并将对象实例a绑定为该函数的第一个参数. foo需要2个参数,而a.foo仅需要1个参数.

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 expects 2 arguments, while a.foo only expects 1 argument.

a绑定到foo.这就是下面的绑定"一词的含义:

a is bound to foo. That is what is meant by the term "bound" below:

print(a.foo)
# <bound method A.foo of <__main__.A object at 0xb7d52f0c>>

对于a.class_fooa不会绑定到class_foo,而是将类A绑定到class_foo.

With a.class_foo, a is not bound to class_foo, rather the class A is bound to class_foo.

print(a.class_foo)
# <bound method type.class_foo of <class '__main__.A'>>

在这里,使用静态方法,即使它是一种方法,a.static_foo也只会返回 一个很好的'ole函数,没有参数绑定. static_foo需要1个参数,并且 a.static_foo也希望有1个参数.

Here, with a staticmethod, even though it is a method, a.static_foo just returns a good 'ole function with no arguments bound. static_foo expects 1 argument, and a.static_foo expects 1 argument too.

print(a.static_foo)
# <function static_foo at 0xb7d479cc>

当然,当您用类A调用static_foo时,也会发生同样的事情.

And of course the same thing happens when you call static_foo with the class A instead.

print(A.static_foo)
# <function static_foo at 0xb7d479cc>

这篇关于静态方法和类方法之间的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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