Skip to content

Instantly share code, notes, and snippets.

@inirudebwoy
Created July 22, 2016 14:22
Show Gist options
  • Save inirudebwoy/ed64d849aed9e2b1c4b14daa10e054c8 to your computer and use it in GitHub Desktop.
Save inirudebwoy/ed64d849aed9e2b1c4b14daa10e054c8 to your computer and use it in GitHub Desktop.
def kwargs_default(params):
"""Decorator for setting default keyword parameters
@kwargs_default((('number_of_chickens', 3),))
will add param 'number_of_chickens' with value of 3 into kwargs.
:param params: tuple of tuples consiting of name of parameter and value
:type params: tuple of tuples with str and any ((str, any), (str, any), ...)
"""
def decorate(func):
def wrapped(*args, **kwargs):
for name, value in params:
if name not in kwargs:
kwargs[name] = value
return func(*args, **kwargs)
return wrapped
return decorate
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment