Created
June 26, 2022 12:59
-
-
Save CGuichard/140538ebb72bfffe66a31ea9b5279d1e to your computer and use it in GitHub Desktop.
Python method which dynamically adds 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
# -*- coding: utf-8 -*- | |
"""Dynamically adds inheritance. | |
Contains the `inherit` method, which dynamically | |
adds inheritance from a parent object to a child object. | |
""" | |
import inspect | |
def inherit(child, parent): | |
_, *child_bases = inspect.getmro(child) | |
_child = type(child.__name__, (parent, *child_bases), child.__dict__.copy()) | |
return _child | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment