I bet not every Python programmer knows what unary operators * and ** do.
Here's the thing: applying * to a list or a tuple extracts all the items of it and forms a function arguments sequence.
Here's the thing: applying * to a list or a tuple extracts all the items of it and forms a function arguments sequence.
That is,
def sum3(a, b, c): return a + b + c threesome = (1, 2, 3) print "sum of", threesome, "is", sum3(*threesome)outputs
sum of (1, 2, 3) is 6.
While ** is used for unpacking a dictionary of named function arguments:
args = { "jumper": "fox", "jumpee": "dog" } print "quick {jumper} jumped over the lazy {jumpee}".\ format(**args)naturally produces
quick fox jumped over the lazy dog.
It's easy to remember, you can think of these operators as the ones literally removing parentheses of a tuple, brackets of a list or curly braces of a dictionary.