Created
October 26, 2016 22:51
-
-
Save kantord/506030d84078aa2c9344eacf9b21893a to your computer and use it in GitHub Desktop.
Use positional arguments to call a function that only takes keyword arguments
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" | |
Example: | |
greet = magic_def("object", object="World")("Hello {object}!".format) | |
greet() # Hello World! | |
greet("John Doe") # Hello John Doe! | |
""" | |
def magic_def(*argument_names, **default_values): | |
def wrapper(func): | |
values = dict(default_values.iteritems()) | |
def wrapped(*args, **kwargs): | |
values.update(dict(zip(argument_names, args))) | |
values.update(kwargs) | |
return func(**values) | |
return wrapped | |
return wrapper |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment