Last active
September 3, 2019 09:07
-
-
Save tomfa/b3d52756f6ad2b5249f6ab9ee1ce05a6 to your computer and use it in GitHub Desktop.
Python inheritance
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
class A: | |
a_attribute = 'A attribute' | |
common_attribute = 'common_attribute A' | |
overriden_attribute = 'overridden_attribute of A' | |
def common_method(self): | |
print(' - calling A.common_method') | |
return f'A.common_method. common_attribute={self.common_attribute}' | |
def a_method(self): | |
print(' - calling A.a_method') | |
return 'A.a_method' | |
def overridden_method(self): | |
print(' - calling A.overridden_method') | |
return 'A.overridden_method' | |
class B: | |
b_attribute = 'B attribute' | |
common_attribute = 'common_attribute B' | |
overriden_attribute = 'overridden_attribute of B' | |
def b_method(self): | |
print(' - calling B.b_method') | |
return 'B.b_method' | |
def common_method(self): | |
print(' - calling B.common_method') | |
return f'B.common_method. common_attribute={self.common_attribute}' | |
def overridden_method(self): | |
print(' - calling B.overridden_method') | |
return 'B.overridden_method' | |
class AB(A, B): | |
ab_attribute = 'AB attribute' | |
overriden_attribute = 'overridden_attribute of AB' | |
def ab_method(self): | |
print(' - calling AB.ab_method') | |
return 'AB.ab_method' | |
def overridden_method(self): | |
print(' - calling AB.overridden_method') | |
return 'AB.overridden_method' | |
def super_common_method(self): | |
return super().common_method() | |
def super_controlled_method(self): | |
self.common_attribute = 'AB overwrote this' | |
return B.common_method(self) | |
class BA(B, A): | |
ba_attribute = 'BA attribute' | |
overriden_attribute = 'overridden_attribute of BA' | |
def ba_method(self): | |
print(' - calling BA.ab_method') | |
return 'BA.ab_method' | |
def overridden_method(self): | |
print(' - calling BA.overridden_method') | |
return 'BA.overridden_method' | |
def super_common_method(self): | |
return super().common_method() | |
def super_controlled_method(self): | |
self.common_attribute = 'BA overwrote this' | |
return A.common_method(self) | |
def test(): | |
methods = [ | |
'common_method', | |
'a_method', | |
'overridden_method', | |
'b_method', | |
'ab_method', | |
'ba_method', | |
'super_common_method', | |
'super_controlled_method', | |
] | |
attributes = [ | |
'a_attribute', | |
'common_attribute', | |
'overriden_attribute', | |
'b_attribute', | |
'ab_attribute', | |
'ba_attribute', | |
] | |
classes = [ | |
AB, BA, A, B | |
] | |
for c in classes: | |
classname = c.__name__ | |
instance = c() | |
print(f'\n') | |
for attr in attributes: | |
print(f'{classname}.{attr}: {getattr(instance, attr, None)}') | |
for attr in methods: | |
method = getattr(instance, attr, None) | |
print(f'{classname}.{attr}: {method and method()}') | |
"""RESULT | |
AB.a_attribute: A attribute | |
AB.common_attribute: common_attribute A | |
AB.overriden_attribute: overridden_attribute of AB | |
AB.b_attribute: B attribute | |
AB.ab_attribute: AB attribute | |
AB.ba_attribute: None | |
- calling A.common_method | |
AB.common_method: A.common_method. common_attribute=common_attribute A | |
- calling A.a_method | |
AB.a_method: A.a_method | |
- calling AB.overridden_method | |
AB.overridden_method: AB.overridden_method | |
- calling B.b_method | |
AB.b_method: B.b_method | |
- calling AB.ab_method | |
AB.ab_method: AB.ab_method | |
AB.ba_method: None | |
- calling A.common_method | |
AB.super_common_method: A.common_method. common_attribute=common_attribute A | |
- calling B.common_method | |
AB.super_controlled_method: B.common_method. common_attribute=AB overwrote this | |
BA.a_attribute: A attribute | |
BA.common_attribute: common_attribute B | |
BA.overriden_attribute: overridden_attribute of BA | |
BA.b_attribute: B attribute | |
BA.ab_attribute: None | |
BA.ba_attribute: BA attribute | |
- calling B.common_method | |
BA.common_method: B.common_method. common_attribute=common_attribute B | |
- calling A.a_method | |
BA.a_method: A.a_method | |
- calling BA.overridden_method | |
BA.overridden_method: BA.overridden_method | |
- calling B.b_method | |
BA.b_method: B.b_method | |
BA.ab_method: None | |
- calling BA.ab_method | |
BA.ba_method: BA.ab_method | |
- calling B.common_method | |
BA.super_common_method: B.common_method. common_attribute=common_attribute B | |
- calling A.common_method | |
BA.super_controlled_method: A.common_method. common_attribute=BA overwrote this | |
A.a_attribute: A attribute | |
A.common_attribute: common_attribute A | |
A.overriden_attribute: overridden_attribute of A | |
A.b_attribute: None | |
A.ab_attribute: None | |
A.ba_attribute: None | |
- calling A.common_method | |
A.common_method: A.common_method. common_attribute=common_attribute A | |
- calling A.a_method | |
A.a_method: A.a_method | |
- calling A.overridden_method | |
A.overridden_method: A.overridden_method | |
A.b_method: None | |
A.ab_method: None | |
A.ba_method: None | |
A.super_common_method: None | |
A.super_controlled_method: None | |
B.a_attribute: None | |
B.common_attribute: common_attribute B | |
B.overriden_attribute: overridden_attribute of B | |
B.b_attribute: B attribute | |
B.ab_attribute: None | |
B.ba_attribute: None | |
- calling B.common_method | |
B.common_method: B.common_method. common_attribute=common_attribute B | |
B.a_method: None | |
- calling B.overridden_method | |
B.overridden_method: B.overridden_method | |
- calling B.b_method | |
B.b_method: B.b_method | |
B.ab_method: None | |
B.ba_method: None | |
B.super_common_method: None | |
B.super_controlled_method: None | |
""" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment