Skip to content

Instantly share code, notes, and snippets.

@CGuichard
Created June 26, 2022 12:59
Show Gist options
  • Save CGuichard/140538ebb72bfffe66a31ea9b5279d1e to your computer and use it in GitHub Desktop.
Save CGuichard/140538ebb72bfffe66a31ea9b5279d1e to your computer and use it in GitHub Desktop.
Python method which dynamically adds inheritance
# -*- 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