Below is examples of passing arguments to python via regular argument assignment, and tuples and *tuples, and dictionaries and **dictionaries.

Here is my gist link for it: https://gist.github.com/anonymous/271d6160cdd90a482b40 (older)

Not covering all methods (for example didnt cover default values if argument is not passed http://lgiordani.com/blog/2015/02/11/default-arguments-in-python/#.VsBpuPIrKHs although default values for none stated argument can be achieved if *arg & *kargs are in the function prototype/definition), but most of them covered in the test script below
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 the script to get better understanding
I ran it like this “python test.py
Im using python 2.7.3

Test Python Script: test.py

################################################################
# 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)
test_var_kwargs(farg=1, **{"myarg2":"twotwo", "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) # imagine this does test_var_args_call(1,"two",3)

################################################################
# 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): # note the function can be redefined without issues
    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) # same as test_var_args(1,2,"two",3)
test=(3,2)
test_var_args_call(*test, **kwargs) # same as test_var_args(3,2,"two",3)

################################################################
# 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) # same as test_var_call3("final",3,2,"two",3)

################################################################
# 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 - will error out
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 - will error out
# test1(5,4,*("asdf","a234",234,543),*(234,643),**{"car":"toyota","price":234},**{"toy":234,"sky":'blue'}) # FAIL - will error out

################################################################
# 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 - will error out
# test2(5,4,("asdf","a234",234,543),(234,643),**{"car":"toyota","price":234}) # FAIL - will error out
# test2(5,4,("asdf","a234",234,543),*(234,643),**{"car":"toyota","price":234}) # FAIL - will error out
# test2(5,4,*("asdf","a234",234,543),*(234,643),**{"car":"toyota","price":234}) # FAIL - will error out
# test2(5,4,*("asdf","a234",234,543),*(234,643),**{"car":"toyota","price":234},**{"toy":234,"sky":'blue'}) # FAIL - will error out
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 - will error out

################################################################
# 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 - will error out
# test2(5,4,("asdf","a234",234,543),(234,643),**{"car":"toyota","price":234}) # FAIL - will error out
# test2(5,4,("asdf","a234",234,543),*(234,643),**{"car":"toyota","price":234}) # FAIL - will error out
# test2(5,4,*("asdf","a234",234,543),*(234,643),**{"car":"toyota","price":234}) # FAIL - will error out
test2(5,4,**{"toy":234,"sky":'blue'}) # FAIL - will error out
# test2(5,4,{"toy":234,"sky":'blue'}) # FAIL - will error out
# test2(5,4,**{"car":"toyota","price":234},**{"toy":234,"sky":'blue'}) # FAIL - will error out
# test2(5,4,*("asdf","a234",234,543),*(234,643)) # FAIL - will error out

## INTERESTING NOTES:
# 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

Output:

# 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_kwargs #########
formal arg: 1
another keyword arg: myarg2: twotwo
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'}

The end

Leave a Reply

Your email address will not be published. Required fields are marked *