简体   繁体   English

Python父/子类方法调用

[英]Python Parent/Child class method call

Python 2.7.6 on Linux. Linux上的Python 2.7.6。

I'm using a test class that inherits from a parent. 我正在使用从父级继承的测试类。 The parent class holds a number of fields that are common to many child classes, and I need to call the parent setUp method to initialize the fields. 父类包含许多子类共有的字段,我需要调用父setUp方法来初始化字段。 Is calling ParentClass.setUp(self) the correct way to do this? 调用ParentClass.setUp(self)正确的方法吗? Here's a simple example: 这是一个简单的例子:

class RESTTest(unittest.TestCase):
    def setUp(self):
        self.host = host
        self.port = port
        self.protocol = protocol
        self.context = context

class HistoryTest(RESTTest):
    def setUp(self):
        RESTTest.setUp(self)
        self.endpoint = history_endpoint
        self.url = "%s://%s:%s/%s/%s" %(self.protocol, self.host, self.port, self.context, self.endpoint)

    def testMe(self):
        self.assertTrue(True)

if __name__ == '__main__':
    unittest.main()

Is this correct? 这个对吗? It seems to work. 它似乎工作。

You would use super for that. 你会用super来做到这一点。

super(ChildClass, self).method(args)

class HistoryTest(RESTTest):
    def setUp(self):
        super(HistoryTest, self).method(args)
        ...

In Python 3 you may write: 在Python 3中,您可以编写:

class HistoryTest(RESTTest):
    def setUp(self):
        super().method(args)
        ...

which is simpler. 这更简单。

See this answer : 看到这个答案

super() lets you avoid referring to the base class explicitly, which can be nice. super()可以避免显式引用基类,这可能很好。 But the main advantage comes with multiple inheritance, where all sorts of fun stuff can happen. 但主要优势在于多重继承,可以发生各种有趣的事情。 See the standard docs on super if you haven't already. 如果您还没有,请参阅super上标准文档

Multiple inheritance 多重继承

To (try to) answer the question in your comment: 要(试着)回答你评论中的问题:

How do you specify which super method you want to call? 如何指定要调用的超级方法?

From what I understand of the philosophy of multiple inheritance (in Python), you don't. 根据我对多重继承哲学的理解(在Python中),你没有。 I mean, super , along with the Method Resolution Order (MRO) should do things right and select the appropriate methods. 我的意思是, super ,以及方法解决顺序(MRO)应该做正确的事情并选择适当的方法。 (Yes methods is a plural, see below.) (是的方法是复数,见下文。)

There are a lot of blog posts / SO answers about this you can find with keywords " multiple inheritance ", " diamond ", " MRO ", " super ", etc. This article provides a Python 3 example I found surprising and didn't find in other sources: 关于这个你可以找到关于“ 多继承 ”,“ 钻石 ”,“ MRO ”,“ 超级 ”等关键字的很多博客帖子/ SO答案。 这篇文章提供了一个我发现令人惊讶的Python 3例子并没有在其他来源找到:

class A:
    def m(self):
        print("m of A called")

class B(A):
    def m(self):
        print("m of B called")
        super().m()

class C(A):
    def m(self):
        print("m of C called")
        super().m()

class D(B,C):
    def m(self):
        print("m of D called")
        super().m()

D().m()

m of D called
m of B called
m of C called
m of A called

See? 看到? Both Bm() and Cm() are called thanks to super , which seems like the right thing to do considering D inherits from both B and C . Bm()Cm()都被称为super ,考虑到D继承自BC ,这似乎是正确的做法。

I suggest you play with this example like I just did. 我建议你像我刚才那样玩这个例子。 Adding a few print s, you'll see that, when calling D().m() , the super().m() statement in class B itself calls Cm() . 添加几个print ,你会看到,当调用D().m()B类中的super().m()语句本身会调用Cm() Whereas, of course, if you call B().m() ( B instance, not D instance), only Am() is called. 当然,如果你调用B().m()B实例,而不是D实例),只调用Am() In other words, super().m() in B is aware of the class of the instance it is dealing with and behaves accordingly. 换句话说, B super().m()知道它正在处理的实例的类,并相应地表现。

Using super everywhere sounds like the silver bullet, but you need to make sure all classes in the inheritance schema are cooperative (another keyword to dig for) and don't break the chain, for instance when expecting additional parameters in child classes. 使用super无处不在听起来像银弹,但你需要确保继承模式中的所有类是合作的 (另一个要挖掘的关键字)并且不要破坏链,例如在期望子类中的其他参数时。

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

相关问题 如何在Python的子类中调用和覆盖父类方法 - How to call and override a Parent class method from Child class in Python 在Python 2中从子类调用父类方法 - Call a parent class method from a child class in Python 2 在python中用子类对象调用父类的重写方法 - Call overridden method of parent class with child class object in python Python父类init调用子级的覆盖方法 - Python parent class init call child's overwritten method Python 如何从父元类调用子 class 方法 - Python How to call child class method from Parent metaclass I want to call parent class method which is overridden in child class through child class object in Python - I want to call parent class method which is overridden in child class through child class object in Python 我们可以在python 2.7中从子类的类方法中调用父类的实例方法吗? - Can we call instance method of parent class from class method of child class in python 2.7? Python从子方法调用父方法 - Python Call Parent Method from child method 如何在python中使用子类的方法调用第二个父类的方法? - How to call method of second parent class using method of child class in python? 如何使用 python 中的子 class 的方法调用第三个或更高的父 class 的方法? - How to call method of third or higher parent class using method of child class in python?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM