Created
December 24, 2014 11:03
-
-
Save NegativeMjark/b091d650769dc14e6dda to your computer and use it in GitHub Desktop.
Bind attributes using decorators and meta classes.
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 bindto(target): | |
def set_attr(name, value): | |
if name == "__new__" and isinstance(target, type): | |
setattr(target, name, staticmethod(value)) | |
else: | |
setattr(target, name, value) | |
def bind_class(cls, name, bases, members): | |
for key, value in members.items(): | |
set_attr(key, value) | |
return target | |
def bind_decorator(cls, thing): | |
if not hasattr(thing, "__name__") and hasattr(thing, "__func__"): | |
set_attr(thing.__func__.__name__, thing) | |
else: | |
set_attr(thing.__name__, thing) | |
Bind = type("Bind", (), {"__call__": bind_decorator})() | |
Bind.__class__.__new__ = staticmethod(bind_class) | |
return Bind |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment