Skip to content

Instantly share code, notes, and snippets.

@shoyer
Created August 30, 2015 23:51
Show Gist options
  • Save shoyer/9a97196c8876cc66c957 to your computer and use it in GitHub Desktop.
Save shoyer/9a97196c8876cc66c957 to your computer and use it in GitHub Desktop.
class partial_by_order(object):
"""
>>> f = partial_by_order(add, [(1, 10)])
>>> f(5)
15
"""
def __init__(self, op, other):
if (not isinstance(other, list) or
not all(isinstance(o, tuple) and len(o) == 2 for o in other)):
raise ValueError('input must be list of tuples')
self.op = op
self.other = other
def __call__(self, *args):
args2 = list(args)
for i, arg in self.other:
args2.insert(i, arg)
return self.op(*args2)
@property
def __name__(self):
if len(self.other) == 1:
other_arg = self.other[0][1]
else:
other_arg = '...'
return '{0}({1})'.format(self.op.__name__, other_arg)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment