Python Call Parent Method Multiple Inheritance

前端 未结 3 981
一个人的身影
一个人的身影 2021-01-04 04:40

So, i have a situation like this.

class A(object):
    def foo(self, call_from):
        print \"foo from A, call from %s\" % call_from


class B(object):
           


        
3条回答
  •  囚心锁ツ
    2021-01-04 04:43

    You can use __bases__ like this

    class D(A, B, C):
        def foo(self):
            print("foo from D")
            for cls in D.__bases__:
                cls().foo("D")
    

    With this change, the output will be

    foo from D
    foo from A, call from D
    foo from B, call from D
    foo from C, call from D
    

提交回复
热议问题