Skip to content

Instantly share code, notes, and snippets.

@willstott101
Last active August 29, 2015 14:21
Show Gist options
  • Save willstott101/10454087f7ba0b5b370b to your computer and use it in GitHub Desktop.
Save willstott101/10454087f7ba0b5b370b to your computer and use it in GitHub Desktop.
from itertools import chain
class DeepPartial(object):
def __init__(self, func, *args, **kwargs):
self.func = func
self.args = args
self.kwargs = kwargs
def _call(self, callee):
if isinstance(callee, self.__class__):
return callee()
else:
return callee
def _call_dpartials(self, args, kwargs):
called = [self._call(a) for a in args]
called_kw = {n: self._call(v) for n, v in kwargs.items()}
return called, called_kw
def _call_children(self):
return self._call_dpartials(self.args, self.kwargs)
def __call__(self, *iargs, **ikwargs):
args, kwargs = self._call_children()
kwargs.update(ikwargs)
return self.func(*chain(args, iargs), **kwargs)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment