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


    背景

    • 有场景分别定义两组逻辑,随后有统一入口做基类属性的整合
    • 其中两组逻辑的积累构造函数定义入参不同
    • 设计类继承图如:

    img

    • 实际的使用方式抽象为[使用] 小节
    • 实际开发过程中遇到问题

    先说结论

    • python 多继承,需要使用super函数进行MRO的依次不重复初始化
    • python 多继承的情况下,构造函数__init__会被依次调用并传递参数
    • python 多继承情况下,__init__参数需要保持一致,否则会出现某些继承路径上的基类初始化遇到异常
    • python 多继承情况下,若构造函数参数不一致,可通过(*args, **kwargs)来统一
    • python 多继承情况下,若有公共基类,MRO可被调整为有跳跃路径,进而利用子类不同的构造函数完成正常初始化,但需要临近基类可以处理子类传来的所有参数。

    使用

        # tjc = TestJobConfiger()
        tjc = SubTest()
        print vars(tjc)
        print "
    ".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"
    
    • 运行结果为:
    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'
    

    二次继承实现

    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
    

    公共基类实现

    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 构造函数的调用,完成基类的初始化
    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"
    
    
    • 运行结果为:
    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
    

    结论

    • 放最前面说吧
  • 相关阅读:
    移动web开发之flex布局(弹性布局)
    移动web开发之流式布局
    移动端基础
    3D变形(CSS3) transform
    动画(CSS3) animation
    2D变形(CSS3) transform
    过渡(CSS3)
    CSS精灵技术(sprite)
    字体图标
    伸缩布局(CSS3)
  • 原文地址:https://www.cnblogs.com/suanec/p/12486326.html
Copyright © 2020-2023  润新知