What is a sibling class in Python?

后端 未结 2 1572
佛祖请我去吃肉
佛祖请我去吃肉 2021-02-02 18:29

Python 2 documentation says that super() function \"returns a proxy object that delegates method calls to a parent or sibling class of type.\"

The questions:

2条回答
  •  闹比i
    闹比i (楼主)
    2021-02-02 18:54

    A sibling is a class with the same parent, as you suspected. The case you're missing is that super could call a sibling method if this class itself is being multiply inherited from:

    class A(object):
      def something(self):
        print("A")
    class B(A):
      def something(self):
        print("B")
    class C(A):
      def something(self):
        print("C, about to call super()")
        super(C, self).something()
    class D(C, B):
      def something(self):
        super(D, self).something()
    
    >>> D().something()
    C, about to call super()
    B
    

    In C, we called super(), but we got B - which is a sibling, not a parent and not the parent of a sibling, but an actual direct sibling of C.

提交回复
热议问题