Skip to content

Instantly share code, notes, and snippets.

@tavin
Created March 15, 2019 17:57
Show Gist options
  • Save tavin/a57b021ef886ddf7327c8d5db5455f14 to your computer and use it in GitHub Desktop.
Save tavin/a57b021ef886ddf7327c8d5db5455f14 to your computer and use it in GitHub Desktop.
wrap a chain of callables
def chain(iterable):
i, f = 0, None
scope = {}
loc = []
for i, f in enumerate(iterable):
name = '_' + str(i)
scope[name] = f
loc.append(name + '(*a,**k)')
if i == 0:
return f
else:
exec('def _(*a,**k):' + ';'.join(loc), scope)
return scope['_']
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment