Skip to content

Instantly share code, notes, and snippets.

@u8sand
Created September 12, 2018 17:40
Show Gist options
  • Save u8sand/e424d6a05752b7a58da9b48ee9e0b6fa to your computer and use it in GitHub Desktop.
Save u8sand/e424d6a05752b7a58da9b48ee9e0b6fa to your computer and use it in GitHub Desktop.
Bind positional and named arguments to function, creating a new function in python
def bind(func, *args, **kwargs):
''' Bind positional and named arguments to `func`.
e.g.
Input:
new_print = bind(print, '>', sep=' ', end='!\n')
Output:
new_print('Test')
> Test!
'''
def func_wrapper(*_args, **_kwargs):
return func(*args, *_args, **dict(kwargs, **_kwargs))
return func_wrapper
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment