Created
August 31, 2021 14:45
-
-
Save dutc/5358862a1905db3948c46d45f35ee75d to your computer and use it in GitHub Desktop.
“Python Expert” Newsletter (Sep 8, 2021): Learning Corner
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: | |
def foo(self): | |
return 'A.foo', | |
class B(A): | |
def foo(self): | |
return 'B.foo', *super().foo() | |
class C(A): | |
def foo(self): | |
return 'C.foo', *super().foo() | |
class D(B, C): | |
def foo(self): | |
return 'D.foo', *super().foo() | |
if __name__ == '__main__': | |
print( | |
f'{A.__mro__ = }', | |
f'{A().foo() = }', | |
f'{B.__mro__ = }', | |
f'{B().foo() = }', | |
f'{C.__mro__ = }', | |
f'{C().foo() = }', | |
f'{D.__mro__ = }', | |
f'{D().foo() = }', | |
sep='\n' | |
) |
For the full write-up and discussion, sign up for the “Python Expert” newsletter!
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
As you can see:
__mro__
) determines the order in whichsuper()
will operatesuper()
is one of the few cases where “what you see is not what you get” in Python (another is class-attribute name-mangling)