Skip to content

Instantly share code, notes, and snippets.

@chrcoe
Created July 15, 2014 15:35
Show Gist options
  • Save chrcoe/afeadbac77ce298380d7 to your computer and use it in GitHub Desktop.
Save chrcoe/afeadbac77ce298380d7 to your computer and use it in GitHub Desktop.
This is a quick snippet to show very basic usage of *args and **kwargs in Python function calls
'''
Snippets for args and kwargs usage, both calling and handling *args and **kwargs
Created on July 15, 2014
@author Chris Coe
'''
def test_args_kwargs(*args, **kwargs):
if args is not None:
for arg in args:
print(arg)
if kwargs is not None:
for k,v in kwargs.items():
print('{} == {}'.format(k,v))
if __name__ == '__main__':
print ('='*25)
# test with naming each arg from param list
test_args_kwargs(test1='test1',test2='test2',test3='test3')
print ('='*25)
# test with passing in a dictionary of k/v pairs
testKWargs = {'test1':'test1','test2':'test2','test3':'test3'}
test_args_kwargs(**testKWargs)
print ('='*25)
# test with passing in direct arguments
test_args_kwargs('test1','test2','test3')
print ('='*25)
# test with passing in list of arguments
testArgs = ['test1','test2','test3']
test_args_kwargs(*testArgs)
print ('='*25)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment