Skip to content

Instantly share code, notes, and snippets.

@stefanocrosta
Created November 28, 2016 14:48
Show Gist options
  • Save stefanocrosta/1a58e020965b38227f3f77b2858fad4a to your computer and use it in GitHub Desktop.
Save stefanocrosta/1a58e020965b38227f3f77b2858fad4a to your computer and use it in GitHub Desktop.
full example of python multiple inheritance with MRO
# see http://stackoverflow.com/a/3277407/422670 and in particular comment by http://stackoverflow.com/users/81636/gatoatigrado
class First(object):
def __init__(self):
print "first prologue"
super(First, self).__init__()
print "first epilogue"
class Second(First):
def __init__(self):
print "second prologue"
super(Second, self).__init__()
print "second epilogue"
class Third(First):
def __init__(self):
print "third prologue"
super(Third, self).__init__()
print "third epilogue"
class Fourth(Second, Third):
def __init__(self):
print "MRO: ", self.__class__.__mro__
print "Fourth __init__"
super(Fourth, self).__init__()
print "that's it"
Fourth()
@stefanocrosta
Copy link
Author

Expected output:

MRO:  (<class '__main__.Fourth'>, <class '__main__.Second'>, <class '__main__.Third'>, <class '__main__.First'>, <type 'object'>)
Fourth __init__
second prologue
third prologue
first prologue
first epilogue
third epilogue
second epilogue
that's it

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment