Created
July 17, 2014 15:36
-
-
Save dplepage/d9c30f7712582ba681b7 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
class Resolver(object): | |
def __init__(self, fn = lambda x:x): | |
self._fn = fn | |
def __getattr__(self, attr): | |
return Resolver(lambda x: getattr(self._fn(x), attr)) | |
def __getitem__(self, key): | |
return Resolver(lambda x: self._fn(x)[key]) | |
def __call__(self, *args, **kwargs): | |
return Resolver(lambda x: self._fn(x)(*args, **kwargs)) | |
def __add__(self, other): | |
return Resolver(lambda x: self._fn(x)+other) | |
def __sub__(self, other): | |
return Resolver(lambda x: self._fn(x)-other) | |
R = Resolver() | |
def apply(res, obj): | |
return res._fn(obj) | |
if __name__ == '__main__': | |
class Foo(object): | |
def bar(self, x): | |
return (x, x+2, x+4) | |
print(apply((R.bar(3)[2]+7) - 1, Foo())) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment