Created
September 7, 2021 09:32
-
-
Save binarycat0/dedb8ca3f2acd56e2401185b760949a5 to your computer and use it in GitHub Desktop.
python set_cls_attrs wrapper
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
def set_cls_attrs(**injected_attrs): | |
def wrap_cls(cls): | |
# inject new class attr | |
for _attr_name, _attr_val in injected_attrs.items(): | |
setattr(cls, _attr_name, _attr_val) | |
# wrapper class based on original cls | |
class wrapper_cls(cls): | |
def __init__(self, *args, **kw): | |
self.original_cls_instance = cls(*args, **kw) | |
# some magic from functools | |
functools.update_wrapper(self, self.original_cls_instance) | |
# for CLI compatible | |
wrapper_cls.__name__ = cls.__name__ | |
return wrapper_cls | |
return wrap_cls |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment