Skip to content

Instantly share code, notes, and snippets.

@redraw
Last active March 5, 2021 23:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save redraw/f94756a2e2d9627d5af33731162b2d81 to your computer and use it in GitHub Desktop.
Save redraw/f94756a2e2d9627d5af33731162b2d81 to your computer and use it in GitHub Desktop.
el sr @ignaciovidal nos dejo manijas para repasar como funcionaba la herencia multiple en Python
>>> class A():
...: def uno(self):
...: print("A uno")
>>> class B():
...: def uno(self):
...: print("B uno")
...: def dos(self):
...: print("B dos")
...:
>>> class Mixin:
...: def uno(self):
...: print("Mixin uno")
...: super().uno()
...: def dos(self):
...: print("Mixin dos")
...: super().dos()
...:
>>> class C(A, B, Mixin):
...: pass
...:
>>> c = C()
>>> c.uno()
A uno
>>> class C(Mixin, A, B):
...: pass
...:
>>> c = C()
>>> c.uno()
Mixin uno
A uno
>>> class C(Mixin, B, A):
...: pass
...:
>>> c = C()
>>> c.uno()
Mixin uno
B uno
>>> class C(Mixin, A, B):
...: pass
...:
>>> c = C()
>>> c.dos()
Mixin dos
B dos
>>> C.__mro__
(__main__.C, __main__.Mixin, __main__.A, __main__.B, object)
# Un ejemplo donde E hereda de otra clase.
# La clase D no tiene metodo dos()
>>> class D:
...: def uno(self):
...: print("D uno")
...:
>>> class E(D):
...: def uno(self):
...: print("E uno")
...: super().uno()
...: def dos(self):
...: print("E dos")
...: super().dos()
...:
>>> class C(E, A, B):
...: pass
...:
>>> c = C()
>>> c.uno()
E uno
D uno
>>> c.dos()
E dos
B dos
>>> C.__mro__
(__main__.C, __main__.E, __main__.D, __main__.A, __main__.B, object)
@ariel-devsar
Copy link

Python Multiple Inheritance – Python MRO (Method Resolution Order)

@redraw
Copy link
Author

redraw commented Mar 5, 2021

Python Multiple Inheritance – Python MRO (Method Resolution Order)

C3 linearization!

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