Skip to content

Instantly share code, notes, and snippets.

@jordotech
Created February 25, 2019 19:58
Show Gist options
  • Save jordotech/539d248810c1d241742e34864160490f to your computer and use it in GitHub Desktop.
Save jordotech/539d248810c1d241742e34864160490f to your computer and use it in GitHub Desktop.
how to use kwargs
"""
You can attach **kwargs as a param to any function, it will allow passing aritrary key=value pairs to the function as parameters.
Its up to you to intercept the kwarg in your receiving function to make use of it, or else it's just ignored
"""
def no_kw_function(id=None, price_over=None, length=None, width=None):
print(id)
print(price_over)
print(length)
print(width)
def kw_function(**kwargs): # you can pass it anything, not strictly defined
print(kwargs)
length_param = kwargs.get('length', None) # check if length came in as a kwarg, default to None
print('length_param: %s' % length_param)
def fire_test1():
return no_kw_function(price_over=1)
def fire_test2():
kw_function(price_over=1, length=10, jarjar='binx', foo='bar')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment