Skip to content

Instantly share code, notes, and snippets.

Created February 14, 2016 11:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anonymous/271d6160cdd90a482b40 to your computer and use it in GitHub Desktop.
Save anonymous/271d6160cdd90a482b40 to your computer and use it in GitHub Desktop.
Showing Different ways to pass arguments to python (including optional)
# python test.py
######### test_var_args #########
formal arg: 1
another arg: two
another arg: 3
######### test_var_kwargs #########
formal arg: 1
another keyword arg: myarg2: two
another keyword arg: myarg3: 3
######### test_var_args_call #########
arg1: 1
arg2: two
arg3: 3
######### test_var_args_call #########
arg1: 1
arg2: two
arg3: 3
######### test_var_args_call #########
arg1: 1
arg2: 2
arg3: two
arg4: 3
######### test_var_args_call #########
arg1: 3
arg2: 2
arg3: two
arg4: 3
######### test_var_args_call #########
arg1: Asd
arg2: 3
arg3: 2
arg4: two
arg5: 3
######### test_var_args_call #########
arg1: final
arg2: 3
arg3: 2
arg4: two
arg5: 3
###### test1 ########
a: a
b: b
arg: ()
karg: {}
###### test1 ########
a: 5
b: 4
arg: (('asdf', 'a234', 234, 543), (234, 643))
karg: {'car': 'toyota', 'price': 234}
###### test1 ########
a: 5
b: 4
arg: (('asdf', 'a234', 234, 543), 234, 643)
karg: {'car': 'toyota', 'price': 234}
###### test2 ########
a: a
b: b
arg: ()
###### test2 ########
a: 5
b: 4
arg: (('asdf', 'a234', 234, 543), 234, 643)
###### test2 ########
a: 5
b: 4
arg: (('asdf', 'a234', 234, 543), ('zebrt', 'lol'), 234, 643)
###### test2 ########
a: 5
b: 4
arg: ('asdf', 'a234', 234, 543)
###### test2 ########
a: a
b: b
kargs: {}
###### test2 ########
a: 5
b: 4
kargs: {'toy': 234, 'sky': 'blue'}
Not covering all methods, but most of them covered.
builds upon:
* http://pythontips.com/2013/08/04/args-and-kwargs-in-python-explained/
* http://stackoverflow.com/questions/9539921/python-function-with-optional-arguments
* and https://gist.github.com/c0ldlimit/4091273 (added test cases to this one)
study the output and play around with it
I ran it like this "python test.py"
Im using python 2.7.3
# cat
################################################################
# This example passes one formal (positional) argument, and two more variable length arguments.
def test_var_args(farg, *args):
print "######### test_var_args #########"
print "formal arg:", farg
for arg in args:
print "another arg:", arg
test_var_args(1, "two", 3)
################################################################
# Here is an example of how to use the keyworded form. Again, one formal argument and two keyworded variable arguments are passed.
def test_var_kwargs(farg, **kwargs):
print "######### test_var_kwargs #########"
print "formal arg:", farg
for key in kwargs:
print "another keyword arg: %s: %s" % (key, kwargs[key])
test_var_kwargs(farg=1, myarg2="two", myarg3=3)
################################################################
# This special syntax can be used, not only in function definitions, but also when calling a function.
def test_var_args_call(arg1, arg2, arg3):
print "######### test_var_args_call #########"
print "arg1:", arg1
print "arg2:", arg2
print "arg3:", arg3
args = ("two", 3)
test_var_args_call(1, *args)
################################################################
# Here is an example using the keyworded form when calling a function:
def test_var_args_call(arg1, arg2, arg3):
print "######### test_var_args_call #########"
print "arg1:", arg1
print "arg2:", arg2
print "arg3:", arg3
kwargs = {"arg3": 3, "arg2": "two"}
test_var_args_call(1, **kwargs)
################################################################
# Here is an example using the keyworded form when calling a function:
def test_var_args_call(arg1, arg2, arg3,arg4):
print "######### test_var_args_call #########"
print "arg1:", arg1
print "arg2:", arg2
print "arg3:", arg3
print "arg4:", arg4
kwargs = {"arg4": 3, "arg3": "two"}
test_var_args_call(1,2, **kwargs)
test=(3,2)
test_var_args_call(*test, **kwargs)
################################################################
# testing all 3
def test_var_args_call3(arg1, arg2, arg3,arg4,arg5):
print "######### test_var_args_call #########"
print "arg1:", arg1
print "arg2:", arg2
print "arg3:", arg3
print "arg4:", arg4
print "arg5:", arg5
kwargs = {"arg5": 3, "arg4": "two"}
test=("Asd",3,2)
test_var_args_call3(*test, **kwargs)
test=(3,2)
test_var_args_call3("final",*test, **kwargs)
################################################################
# testing optional vars: a,b are needed everything else optional
def test1(a,b,*arg,**kargs):
print "###### test1 ########"
print "a:", a
print "b:", b
print "arg:", arg
print "karg:", kargs
test1("a","b")
# test1("a") # FAIL
test1(5,4,("asdf","a234",234,543),(234,643),**{"car":"toyota","price":234})
test1(5,4,("asdf","a234",234,543),*(234,643),**{"car":"toyota","price":234})
# test1(5,4,*("asdf","a234",234,543),*(234,643),**{"car":"toyota","price":234}) # FAIL
# test1(5,4,*("asdf","a234",234,543),*(234,643),**{"car":"toyota","price":234},**{"toy":234,"sky":'blue'}) # FAIL
################################################################
# testing optional vars: a,b are needed everything else optional
def test2(a,b,*arg):
print "###### test2 ########"
print "a:", a
print "b:", b
print "arg:", arg
test2("a","b")
# test1("a") # FAIL
# test2(5,4,("asdf","a234",234,543),(234,643),**{"car":"toyota","price":234}) # FAIL
# test2(5,4,("asdf","a234",234,543),*(234,643),**{"car":"toyota","price":234}) # FAIL
# test2(5,4,*("asdf","a234",234,543),*(234,643),**{"car":"toyota","price":234}) # FAIL
# test2(5,4,*("asdf","a234",234,543),*(234,643),**{"car":"toyota","price":234},**{"toy":234,"sky":'blue'}) # FAIL
test2(5,4,("asdf","a234",234,543),*(234,643))
test2(5,4,("asdf","a234",234,543),("zebrt","lol"),*(234,643))
test2(5,4,*("asdf","a234",234,543))
# test2(5,4,*("asdf","a234",234,543),*(234,643)) # FAIL
################################################################
# testing optional vars: a,b are needed everything else optional
def test2(a,b,**kargs):
print "###### test2 ########"
print "a:", a
print "b:", b
print "kargs:", kargs
test2("a","b")
# test2("a") # FAIL
# test2(5,4,("asdf","a234",234,543),(234,643),**{"car":"toyota","price":234}) # FAIL
# test2(5,4,("asdf","a234",234,543),*(234,643),**{"car":"toyota","price":234}) # FAIL
# test2(5,4,*("asdf","a234",234,543),*(234,643),**{"car":"toyota","price":234}) # FAIL
test2(5,4,**{"toy":234,"sky":'blue'}) # FAIL
# test2(5,4,{"toy":234,"sky":'blue'}) # FAIL
# test2(5,4,**{"car":"toyota","price":234},**{"toy":234,"sky":'blue'}) # FAIL
# test2(5,4,*("asdf","a234",234,543),*(234,643)) # FAIL
## summary:
# only 1 *(a,b,c) can be passed as an arg
# only 1 **{dictionary} can be passed as kwarg
# multiple (a,b,c), can be passed as an arg
# {a,b,c} are passed as a variable and fail because our first 2 none-optional args are taken
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment