[英]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上的标准文档 。
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继承自B
和C
,这似乎是正确的做法。
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.