python多继承构造函数声明问题

前言:

嗨喽~大家好呀,这里是魔王呐 ❤ ~!

python更多源码/资料/解答/教程等 点击此处跳转文末名片免费获取

背景

有场景分别定义两组逻辑,随后有统一入口做基类属性的整合

其中两组逻辑的积累构造函数定义入参不同

设计类继承图如:

python多继承构造函数声明问题_第1张图片

  • 实际的使用方式抽象为[使用] 小节

  • 实际开发过程中遇到问题

先说结论

  1. python 多继承,需要使用super函数进行MRO的依次不重复初始化

  2. python 多继承的情况下,构造函数__init__会被依次调用并传递参数

  3. python 多继承情况下,__init__参数需要保持一致,否则会出现某些继承路径上的基类初始化遇到异常

  4. python 多继承情况下,若构造函数参数不一致,可通过(*args, **kwargs)来统一

  5. python 多继承情况下,若有公共基类,MRO可被调整为有跳跃路径,进而利用子类不同的构造函数完成正常初始化,但需要临近基类可以处理子类传来的所有参数。

使用

'''
遇到问题没人解答?小编创建了一个Python学习交流QQ群:926207505
寻找有志同道合的小伙伴,互帮互助,群里还有不错的视频学习教程和PDF电子书!
'''
# tjc = TestJobConfiger()
tjc = SubTest()
print vars(tjc)
print "\n".join([tjc.base_key,
                tjc.base1_key,
                tjc.subbase_key,
                tjc.subbase1_key,
                tjc.subtest_key])

期望结果

enter Base
enter SubBase
enter Base1|_arg1 |_arg2 
enter SubBase1|_arg1 |_arg2 
vars : {'base1_key': 'base1_key', 'subtest_key': 'subtest_key', 'subbase1_key': 'subbase1_key', 'subbase_key': 'subbase_key', 'base_key': 'base_key'}
values: base_key
base1_key
subbase_key
subbase1_key
subtest_key

第一版实现

class Base(object):
    def __init__(self):
        print "enter Base"
        self.base_key = "base_key"

class Base1(object):
    def __init__(self, _arg1 = "_arg1 ", _arg2 = "_arg2 "):
        print "enter Base1" + "|" +  _arg1 + "|" + _arg2
        self.base1_key = "base1_key"

class SubBase(Base):
    def __init__(self):
        super(SubBase, self).__init__()
        print "enter SubBase"
        self.subbase_key = "subbase_key"

class SubBase1(Base1):
    def __init__(self, _arg1 = "_arg1 ", _arg2 = "_arg2 "):
        super(SubBase1, self).__init__(_arg1 = _arg1, _arg2=_arg2)
        print "enter SubBase1" + "|" +  _arg1 + "|" + _arg2
        self.subbase1_key = "subbase1_key"

class SubTest(SubBase,SubBase1):
    def __init__(self, _arg1 = "_arg1 "):
        super(SubTest, self).__init__(_arg1=_arg1, _arg2="None")
        # self.__dict__.update(vars(SubBase()))
        self.subtest_key = "subtest_key"

运行结果为:

'''
遇到问题没人解答?小编创建了一个Python学习交流QQ群:926207505
寻找有志同道合的小伙伴,互帮互助,群里还有不错的视频学习教程和PDF电子书!
'''
Traceback (most recent call last):
  File "/Users/enzhao/suanec/ksp/dispatch/weiclient/client/weiclient/libs/com/weibo/tools/job_manager/job_configer_tester.py", line 43, in <module>
    tjc = SubTest()
  File "/Users/enzhao/suanec/ksp/dispatch/weiclient/client/weiclient/libs/com/weibo/tools/job_manager/job_configer_tester.py", line 38, in __init__
    super(SubTest, self).__init__(_arg1=_arg1, _arg2="None")
TypeError: __init__() got an unexpected keyword argument '_arg1'

第二版实现

class Base(object):
    def __init__(self):
        print "enter Base"
        self.base_key = "base_key"

class Base1(object):
    def __init__(self, _arg1 = "_arg1 ", _arg2 = "_arg2 "):
        print "enter Base1" + "|" +  _arg1 + "|" + _arg2
        self.base1_key = "base1_key"

class SubBase(Base):
    def __init__(self, **kwargs):
        super(SubBase, self).__init__()
        print "enter SubBase"
        self.subbase_key = "subbase_key"

class SubBase1(Base1):
    def __init__(self, _arg1 = "_arg1 ", _arg2 = "_arg2 "):
        super(SubBase1, self).__init__(_arg1 = _arg1, _arg2=_arg2)
        print "enter SubBase1" + "|" +  _arg1 + "|" + _arg2
        self.subbase1_key = "subbase1_key"

class SubTest(SubBase,SubBase1):
    def __init__(self, _arg1 = "_arg1 "):
        super(SubTest, self).__init__(_arg1=_arg1, _arg2="None")
        # self.__dict__.update(vars(SubBase()))
        self.subtest_key = "subtest_key"

运行结果为:

Traceback (most recent call last):
enter Base
enter SubBase
  File "/Users/enzhao/suanec/ksp/dispatch/weiclient/client/weiclient/libs/com/weibo/tools/job_manager/job_configer_tester.py", line 46, in <module>
vars : {'subtest_key': 'subtest_key', 'subbase_key': 'subbase_key', 'base_key': 'base_key'}
    tjc.base1_key,
AttributeError: 'SubTest' object has no attribute 'base1_key'

二次继承实现

'''
遇到问题没人解答?小编创建了一个Python学习交流QQ群:926207505
寻找有志同道合的小伙伴,互帮互助,群里还有不错的视频学习教程和PDF电子书!
'''
class Base(object):
    def __init__(self):
        print "enter Base"
        self.base_key = "base_key"

class Base1(Base):
    def __init__(self, _arg1 = "_arg1 ", _arg2 = "_arg2 "):
        super(Base1, self).__init__()
        print "enter Base1" + "|" +  _arg1 + "|" + _arg2
        self.base1_key = "base1_key"

class SubBase(Base):
    def __init__(self, **kwargs):
        super(SubBase, self).__init__()
        print "enter SubBase"
        self.subbase_key = "subbase_key"

class SubBase1(Base1):
    def __init__(self, _arg1 = "_arg1 ", _arg2 = "_arg2 "):
        super(SubBase1, self).__init__(_arg1 = _arg1, _arg2=_arg2)
        print "enter SubBase1" + "|" +  _arg1 + "|" + _arg2
        self.subbase1_key = "subbase1_key"

class SubTest(SubBase,SubBase1):
    def __init__(self, _arg1 = "_arg1 "):
        super(SubTest, self).__init__(_arg1=_arg1, _arg2="None")
        # self.__dict__.update(vars(SubBase()))
        self.subtest_key = "subtest_key"

运行结果为:

enter Base
enter Base1|_arg1 |_arg2 
enter SubBase1|_arg1 |_arg2 
enter SubBase
vars : {'base1_key': 'base1_key', 'subtest_key': 'subtest_key', 'subbase_key': 'subbase_key', 'subbase1_key': 'subbase1_key', 'base_key': 'base_key'}
values: base_key
base1_key
subbase_key
subbase1_key
subtest_key

公共基类实现

'''
遇到问题没人解答?小编创建了一个Python学习交流QQ群:926207505
寻找有志同道合的小伙伴,互帮互助,群里还有不错的视频学习教程和PDF电子书!
'''
class BBase(object):
    def __init__(self):
        pass

class Base(BBase):
    def __init__(self):
        print "enter Base"
        self.base_key = "base_key"

class Base1(BBase):
    def __init__(self, _arg1 = "_arg1 ", _arg2 = "_arg2 "):
        print "enter Base1" + "|" +  _arg1 + "|" + _arg2
        self.base1_key = "base1_key"

class SubBase(Base):
    def __init__(self, **kwargs):
        super(SubBase, self).__init__()
        print "enter SubBase"
        self.subbase_key = "subbase_key"

class SubBase1(Base1):
    def __init__(self, _arg1 = "_arg1 ", _arg2 = "_arg2 "):
        super(SubBase1, self).__init__(_arg1 = _arg1, _arg2=_arg2)
        print "enter SubBase1" + "|" +  _arg1 + "|" + _arg2
        self.subbase1_key = "subbase1_key"

class SubTest(SubBase,SubBase1):
    def __init__(self, _arg1 = "_arg1 "):
        super(SubTest, self).__init__(_arg1=_arg1, _arg2="None")
        # self.__dict__.update(vars(SubBase()))
        self.subtest_key = "subtest_key"

运行结果为:

enter Base
enter SubBase
vars : {'subtest_key': 'subtest_key', 'subbase_key': 'subbase_key', 'base_key': 'base_key'}
Traceback (most recent call last):
  File "/Users/enzhao/suanec/ksp/dispatch/weiclient/client/weiclient/libs/com/weibo/tools/job_manager/job_configer_tester.py", line 50, in <module>
    tjc.base1_key,
AttributeError: 'SubTest' object has no attribute 'base1_key'

Hack实现

分别保持各自定义逻辑

在入口子类中,声明冲突基类的对象,利用python的vars和__dict__的特性进行属性的声明

相当于手动hard-code 构造函数的调用,完成基类的初始化

'''
遇到问题没人解答?小编创建了一个Python学习交流QQ群:926207505
寻找有志同道合的小伙伴,互帮互助,群里还有不错的视频学习教程和PDF电子书!
'''
class Base(object):
    def __init__(self):
        print "enter Base"
        self.base_key = "base_key"

class Base1(object):
    def __init__(self, _arg1 = "_arg1 ", _arg2 = "_arg2 "):
        print "enter Base1" + "|" +  _arg1 + "|" + _arg2
        self.base1_key = "base1_key"

class SubBase(Base):
    def __init__(self):
        super(SubBase, self).__init__()
        print "enter SubBase"
        self.subbase_key = "subbase_key"

class SubBase1(Base1):
    def __init__(self, _arg1 = "_arg1 ", _arg2 = "_arg2 "):
        super(SubBase1, self).__init__(_arg1 = _arg1, _arg2=_arg2)
        print "enter SubBase1" + "|" +  _arg1 + "|" + _arg2
        self.subbase1_key = "subbase1_key"

class SubTest(SubBase1):
    def __init__(self, _arg1 = "_arg1 "):
        super(SubTest, self).__init__(_arg1=_arg1, _arg2="None")
        self.__dict__.update(vars(SubBase()))
        self.subtest_key = "subtest_key"

运行结果为:

'''
遇到问题没人解答?小编创建了一个Python学习交流QQ群:926207505
寻找有志同道合的小伙伴,互帮互助,群里还有不错的视频学习教程和PDF电子书!
'''
enter Base1|_arg1 |None
enter SubBase1|_arg1 |None
enter Base
enter SubBase
vars : {'base1_key': 'base1_key', 'subtest_key': 'subtest_key', 'subbase_key': 'subbase_key', 'subbase1_key': 'subbase1_key', 'base_key': 'base_key'}
values: base_key
base1_key
subbase_key
subbase1_key
subtest_key

官方建议实现

class A(object):
    def __init__(self, *args, **kwargs):
        print "A"

class B(object):
    def __init__(self, *args, **kwargs):
        print "B"

class C(A):
    def __init__(self, arg, *args, **kwargs):
        print "C","arg=",arg
        super(C, self).__init__(arg, *args, **kwargs)

class D(B):
    def __init__(self, arg, *args, **kwargs):
        print "D", "arg=",arg
        super(D, self).__init__(arg, *args, **kwargs)

class E(C,D):
    def __init__(self, arg, *args, **kwargs):
        print "E", "arg=",arg
        super(E, self).__init__(arg, *args, **kwargs)

print "MRO:", [x.__name__ for x in E.__mro__]
E(10)
MRO: ['E', 'C', 'A', 'D', 'B', 'object']
E arg= 10
C arg= 10
A

尾语

最后感谢你观看我的文章呐~本次航班到这里就结束啦

希望本篇文章有对你带来帮助 ,有学习到一点知识~

躲起来的星星也在努力发光,你也要努力加油(让我们一起努力叭)。

最后,宣传一下呀~更多源码、资料、素材、解答、交流皆点击下方名片获取呀

你可能感兴趣的:(python,python,开发语言,pycharm,学习)