Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
def extend_instance(instance, *bases, **kwargs):
"""
Apply subclass (mixin) to a class object or its instance
By default, the mixin is placed at the start of bases
to ensure its called first as per MRO. If you wish to
have it injected last, which is useful for monkeypatching,
then you can specify 'last=True'. See here:
http://stackoverflow.com/a/10018792/1267398
:attr cls: Target object
:type cls: Class instance
:attr bases: List of new bases to subclass with
:attr last: Inject new bases after existing bases
:type last: bool
>>> class A(object): pass
>>> class B(object): pass
>>> a = A()
>>> b = B()
>>> isinstance(b, A)
False
>>> extend_instance(b, A)
>>> isinstance(b, A)
True
"""
last = kwargs.get('last', False)
bases = tuple(bases)
for base in bases:
assert inspect.isclass(base), "bases must be classes"
assert not inspect.isclass(instance)
base_cls = instance.__class__
base_cls_name = instance.__class__.__name__
new_bases = (base_cls,)+bases if last else bases+(base_cls,)
new_cls = type(base_cls_name, tuple(new_bases), {})
setattr(instance, '__class__', new_cls)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment