简体   繁体   English

在 Python 中调用基类的类方法

[英]Calling a base class's classmethod in Python

Consider the following code:考虑以下代码:

class Base(object):

    @classmethod
    def do(cls, a):
        print cls, a

class Derived(Base):

    @classmethod
    def do(cls, a):
        print 'In derived!'
        # Base.do(cls, a) -- can't pass `cls`
        Base.do(a)

if __name__ == '__main__':
    d = Derived()
    d.do('hello')

> $ python play.py  
> In derived! 
> <class '__main__.Base'> msg

From Derived.do , how do I call Base.do ?Derived.do ,我如何调用Base.do

I would normally use super or even the base class name directly if this is a normal object method, but apparently I can't find a way to call the classmethod in the base class.如果这是一个普通的对象方法,我通常会直接使用super甚至基类名称,但显然我找不到在基类中调用 classmethod 的方法。

In the above example, Base.do(a) prints Base class instead of Derived class.在上面的例子中, Base.do(a)打印Base class 而不是Derived class。

If you're using a new-style class (ie derives from object in Python 2, or always in Python 3), you can do it with super() like this:如果您使用的是新式类(即派生自 Python 2 中的object ,或始终在 Python 3 中),您可以像这样使用super()

super(Derived, cls).do(a)

This is how you would invoke the code in the base class's version of the method (ie print cls, a ), from the derived class, with cls being set to the derived class.这就是您将如何从派生类调用基类版本的方法(即print cls, a )中的代码,并将cls设置为派生类。

this has been a while, but I think I may have found an answer.这已经有一段时间了,但我想我可能已经找到了答案。 When you decorate a method to become a classmethod the original unbound method is stored in a property named 'im_func':当你装饰一个方法成为一个类方法时,原始未绑定的方法存储在一个名为“im_func”的属性中:

class Base(object):
    @classmethod
    def do(cls, a):
        print cls, a

class Derived(Base):

    @classmethod
    def do(cls, a):
        print 'In derived!'
        # Base.do(cls, a) -- can't pass `cls`
        Base.do.im_func(cls, a)

if __name__ == '__main__':
    d = Derived()
    d.do('hello')

Building on the answer from @David Z using:基于@David Z 的回答,使用:

super(Derived, cls).do(a)

Which can be further simplified to:可以进一步简化为:

super(cls, cls).do(a)

I often use classmethods to provide alternative ways to construct my objects.我经常使用类方法来提供构造对象的替代方法。 In the example below I use the super functions as above for the class method load that alters the way that the objects are created:在下面的示例中,我将上面的超级函数用于类方法加载,它改变了创建对象的方式:

class Base():
    
    def __init__(self,a):
        self.a = a
    
    @classmethod
    def load(cls,a):
        return cls(a=a)
    
class SubBase(Base): 

    @classmethod
    def load(cls,b):
        a = b-1
        return super(cls,cls).load(a=a)
    
base = Base.load(a=1)
print(base)
print(base.a)

sub = SubBase.load(b=3)
print(sub)
print(sub.a)

Output:输出:

<__main__.Base object at 0x128E48B0>
1
<__main__.SubBase object at 0x128E4710>
2

这对我有用:

Base.do('hi')

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM