Last active
March 3, 2019 20:57
-
-
Save Sean-Bradley/d6cee6435c56b7ddfd0e9c65eaa7713f to your computer and use it in GitHub Desktop.
Testing Python Inheritance Override Order
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Foo: | |
def testOverideOrder(self): | |
print("Foo") | |
class Bar: | |
def testOverideOrder(self): | |
print("Bar") | |
class TestFooBar(Foo, Bar): | |
pass | |
class TestBarFoo(Bar, Foo): | |
pass | |
testFooBar = TestFooBar() | |
testBarFoo = TestBarFoo() | |
testFooBar.testOverideOrder() | |
testBarFoo.testOverideOrder() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
output from script
By running the attached function you'll see that the order of inheritance in a python class declaration matters.
The 1st inherited class method overrides any identical method signatures in any further inherited classes .
The order of precedence is left to right.
So, in case of calling the
testFooBar.testOverideOrder()
method, the inherited Foo'stestOverideOrder
method is called,and in case of calling the
testBarFoo.testOverideOrder()
method, the inherited Bar'stestOverideOrder
method is called.