Last active
January 23, 2023 01:23
-
-
Save juanarrivillaga/0d4d2d489347777bc21f9e150149b093 to your computer and use it in GitHub Desktop.
An example of how we could implement a basic `super` in pure Python
This file contains hidden or 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 SimpleSuper: | |
def __init__(self, cls, instance): | |
self.cls = cls | |
self.instance = instance | |
def __getattr__(self, name): | |
mro = type(self.instance).mro() | |
next_cls = mro[mro.index(self.cls) + 1] | |
attribute = getattr(next_cls, name) | |
if hasattr(attribute, "__get__"): | |
return attribute.__get__(self.instance, self.cls) | |
return attribute |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment