Created
January 21, 2016 09:23
-
-
Save foxx/5807755d92a76970dae6 to your computer and use it in GitHub Desktop.
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
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