Created
October 8, 2019 14:37
-
-
Save pmundt/5a8937190afbf808d47140d07caf2e75 to your computer and use it in GitHub Desktop.
Python MRO and Multi-class inheritance type clobbering
This file contains 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
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