Skip to content

Instantly share code, notes, and snippets.

@kantord
Created October 26, 2016 22:51
Show Gist options
  • Save kantord/506030d84078aa2c9344eacf9b21893a to your computer and use it in GitHub Desktop.
Save kantord/506030d84078aa2c9344eacf9b21893a to your computer and use it in GitHub Desktop.
Use positional arguments to call a function that only takes keyword arguments
"""
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