Python3 - Multiple Inheritances

class First(object):
    def __init__(self, a1, *args, **kwargs):
        # super(First, self).__init__(*args, **kwargs)
        print("First: a1:{0}".format(a1))
        print("First: args: ", args)
        print("First: kwargs: ", kwargs)

class Second(First):
    def __init__(self, a2, b2, *args, **kwargs):
        super(Second, self).__init__(*args, **kwargs)
        print("Second: a2:{0} b2:{1}".format(a2, b2))
        print("Second: args: ", args)
        print("Second: kwargs: ", kwargs)

class Third(First):
    def __init__(self, a3, b3, c3, *args, **kwargs):
        super(Third, self).__init__(*args, **kwargs)
        print("Third: a3:{0} b3:{1} c3:{2}".format(a3, b3, c3))
        print("Third: args: ", args)
        print("Third: kwargs: ", kwargs)

class Fourth(Second, Third):
    def __init__(self, *args, **kwargs):
        super(Fourth, self).__init__(*args, **kwargs)
        print("Fourth: args: ", args)
        print("Fourth: kwargs: ", kwargs)
        print("Fourth: that's it")

In [66]: Fourth.__mro__

Out[66]: (__main__.Fourth, __main__.Second, __main__.Third, __main__.First, object)

In [76]: Fourth(101, 201, 202, 301, 302, 303)

First: a1:303
First: args:  ()
First: kwargs:  {}
Third: a3:202 b3:301 c3:302
Third: args:  (303,)
Third: kwargs:  {}
Second: a2:101 b2:201
Second: args:  (202, 301, 302, 303)
Second: kwargs:  {}
Fourth: args:  (101, 201, 202, 301, 302, 303)
Fourth: kwargs:  {}
Fourth: that's it
Out[76]: <__main__.Fourth at 0x6a7d550>

In [77]: Fourth(101, 201, 202, 301, 302, 303, 401, 402, 403, 404, m1=501, m2=502)

First: a1:303
First: args:  (401, 402, 403, 404)
First: kwargs:  {'m1': 501, 'm2': 502}
Third: a3:202 b3:301 c3:302
Third: args:  (303, 401, 402, 403, 404)
Third: kwargs:  {'m1': 501, 'm2': 502}
Second: a2:101 b2:201
Second: args:  (202, 301, 302, 303, 401, 402, 403, 404)
Second: kwargs:  {'m1': 501, 'm2': 502}
Fourth: args:  (101, 201, 202, 301, 302, 303, 401, 402, 403, 404)
Fourth: kwargs:  {'m1': 501, 'm2': 502}
Fourth: that's it
Out[77]: <__main__.Fourth at 0x6a7dd68>

In [79]: Fourth(a2=101, c=10, d=100, a1=201, b2=202, a3=301, b3=302, c3=303, x=200)

First: a1:201
First: args:  ()
First: kwargs:  {'c': 10, 'd': 100, 'x': 200}
Third: a3:301 b3:302 c3:303
Third: args:  ()
Third: kwargs:  {'c': 10, 'd': 100, 'a1': 201, 'x': 200}
Second: a2:101 b2:202
Second: args:  ()
Second: kwargs:  {'c': 10, 'd': 100, 'a1': 201, 'a3': 301, 'b3': 302, 'c3': 303, 'x': 200}
Fourth: args:  ()
Fourth: kwargs:  {'a2': 101, 'c': 10, 'd': 100, 'a1': 201, 'b2': 202, 'a3': 301, 'b3': 302, 'c3': 303, 'x': 200}
Fourth: that's it
Out[79]: <__main__.Fourth at 0x69909b0>

According to this article about Method Resolution Order

Deep Thoughts by Raymond Hettinger

转载于:https://my.oschina.net/zungyiu/blog/1944327

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值