The difference is how the arguments are passed to the called functions. When you use * , the arguments are unpacked (if they are a list or a tuple); otherwise, they are simply transmitted as is.
Here is an example of the difference:
>>> def add(a, b): ... print a + b ... >>> add(*[2,3]) 5 >>> add([2,3]) Traceback (most recent call last): File "<stdin>", line 1, in <module> TypeError: add() takes exactly 2 arguments (1 given) >>> add(4, 5) 9
When I prefix the argument * , it actually unpacked the list into two separate arguments, which were passed to add as a and b . Without it, it is simply passed in the list as one argument.
The same applies to dictionaries and ** , except that they are passed as names, not ordered arguments.
>>> def show_two_stars(first, second='second', third='third'): ... print "first: " + str(first) ... print "second: " + str(second) ... print "third: " + str(third) >>> show_two_stars('a', 'b', 'c') first: a second: b third: c >>> show_two_stars(**{'second': 'hey', 'first': 'you'}) first: you second: hey third: third >>> show_two_stars({'second': 'hey', 'first': 'you'}) first: {'second': 'hey', 'first': 'you'} second: second third: third
jdotjdot
source share