Skip to content

Instantly share code, notes, and snippets.

@dplepage
Created July 17, 2014 15:36
Show Gist options
  • Save dplepage/d9c30f7712582ba681b7 to your computer and use it in GitHub Desktop.
Save dplepage/d9c30f7712582ba681b7 to your computer and use it in GitHub Desktop.
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