Skip to content

Instantly share code, notes, and snippets.

@aladinoster
Last active August 9, 2021 09:22
Show Gist options
  • Save aladinoster/a719823bff8876397277b109b4b7c9f3 to your computer and use it in GitHub Desktop.
Save aladinoster/a719823bff8876397277b109b4b7c9f3 to your computer and use it in GitHub Desktop.
Python: Multiple inheritance with different signatures
""" Different signature multiple inheritance
"""
# Explicit is better than implicit
class A:
def __init__(self, a, b):
print(
"Init {} with arguments {}".format(self.__class__.__name__, (a, b))
)
class B:
def __init__(self, q):
print("Init {} with arguments {}".format(self.__class__.__name__, (q)))
class C(A, B):
def __init__(self):
# Unbound functions, so pass in self explicitly
A.__init__(self, 1, 2)
B.__init__(self, 3)
# Using super: Super considers a mecanism via the mro so consider a class that support all arguments. Here you rely on the mro so it may be confusing if you have some similarity in between signatures.
class D:
def __init__(self, a=None, b=None, *args, **kwargs):
super().__init__(*args, **kwargs)
print(
"Init {} with arguments {}".format(self.__class__.__name__, (a, b))
)
class E:
def __init__(self, q=None, *args, **kwargs):
super().__init__(*args, **kwargs)
print("Init {} with arguments {}".format(self.__class__.__name__, (q)))
class F(D, E):
def __init__(self):
super().__init__(a=1, b=2, q=3)
if __name__ == "__main__":
c = C()
print(C.mro())
f = F()
print(F.mro())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment