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 attr_to_arg(func): | |
""" | |
This is stupid and I love it. | |
Basically instead of calling: | |
foo("thing", stuff) | |
You can call: | |
foo.thing(stuff) | |
once you apply this decorator to foo. | |
Works on methods, unless you try using the 'unbound' method. | |
""" | |
class AttrGetter: | |
def __init__(self): | |
self.instance = None | |
def __getattr__(self, item): | |
return Caller(self.instance, item) | |
def __get__(self, instance, owner): | |
self.instance = instance | |
return self | |
class Caller: | |
def __init__(self, instance, item): | |
self.instance = instance | |
self.item = item | |
def __call__(self, *args, **kwargs): | |
if self.instance is None: | |
return func(self.item, *args, **kwargs) | |
else: | |
return func(self.instance, self.item, *args, **kwargs) | |
return AttrGetter() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment