Skip to content

Instantly share code, notes, and snippets.

@pmundt
Created October 8, 2019 14:37
Show Gist options
  • Save pmundt/5a8937190afbf808d47140d07caf2e75 to your computer and use it in GitHub Desktop.
Save pmundt/5a8937190afbf808d47140d07caf2e75 to your computer and use it in GitHub Desktop.
Python MRO and Multi-class inheritance type clobbering
from abc import ABC, abstractmethod
from datetime import timedelta
class A1(ABC):
_value = "A1"
def __init__(self):
print("A1 type at init:", type(self._value))
def test_value(self):
assert type(self._value) is not str
@abstractmethod
def print_value(self):
pass
class A2(ABC):
_value = timedelta()
def __init__(self):
pass
def print_value(self):
print('A2 type:', type(self._value))
print(self._value)
class B(A1, A2):
_value = 1
def __init__(self):
super().__init__()
def print_type(self):
print('B type:', type(self._value))
def print_value(self):
print('B type:', type(self._value))
print(self._value)
def main():
b = B()
b.print_value()
b.print_type()
b.test_value()
print(B.mro())
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment