【发布时间】:2017-04-21 00:49:20
【问题描述】:
我遇到了一些关于 python 的多重继承的问题。
class TestFather(object):
fathers = {}
path = "/C"
def __init__(self):
super(TestFather, self).__init__()
# self.fathers = file_to_dict(self.path)
class TestMother(object):
mothers = {}
path = "/D"
def __init__(self):
super(TestMother, self).__init__()
# self.mothers = file_to_dict(self.path)
class TestChild(TestFather, TestMother):
def __init__(self):
super(TestChild, self).__init__()
t = TestChild()
help(t)
变量路径将存储母亲和父亲的文件目录。当我打印出父亲和母亲的词典时,我注意到母亲词典不知何故从父亲那里获取了所有信息。在阅读了Guido关于MRO http://python-history.blogspot.com/2010/06/method-resolution-order.html的博客并观看了Raymond Hettinger 2015 PyCon视频超级棒后,我意识到TestChild类只继承了TestFather的path变量,而完全忽略了TestMother的path变量。
我的问题是,有没有办法让 TestChild 使用其两个父母各自的路径,而不是只采用具有更高 MRO 的路径。 我知道更改变量名可以解决问题,但作为雷蒙德说,一定有更好的办法。
【问题讨论】:
标签: python inheritance method-resolution-order