1

Is there a (meaningful) difference the two classes below?

class A(Foo, Bar):
    ...

vs

class B(Bar, Foo):
    ...
theEpsilon
  • 1,800
  • 17
  • 30
  • short answer: yes. Google "Python MRO" for details. (But it won't if `Foo` and `Bar` have no method names in common, and don't have any common base classes.) – Robin Zigmond Sep 22 '20 at 21:52

1 Answers1

2

Does order of class bases in python matter?

Yes, it does. Consider the following:

class A:
    def foo(self):
        print('class A')
class B:
    def foo(self):
        print('class B')
class C(A, B):
    pass

c = C()
c.foo() # class A

Methods are resolved from left-to-right—e.g., in the example, the foo method comes from class A because A comes before B.

  • Thanks, but notice this doesn't make sense because super().__init__() calls are the opposite order of this I think. This means inheritance has L2R priority in the base class list but R2L priority initializing. So you're going to override methods but if you setup data members then it's the opposite. That's bound to develop into bugs. – MathCrackExchange Jul 08 '23 at 01:19