(star) *
(Star) * Operator
The single star * unpacks the sequence/collection into positional arguments, so you can do this:
def sum(a, b):
return a + b
values = (1, 2)
s = sum(*values)
print(s)
s = sum(1, 2) #The same
print(s)
3
3
values = { 'a': 1, 'b': 2 }
s = sum(**values)
print(s)
3
def sum(a, b, c, d):
return a + b + c + d
values1 = (1, 2)
values2 = { 'c': 10, 'd': 15 }
s = sum(*values1, **values2)
print(s)
# the same as
s = sum(1, 2, c=10, d=15)
print(s)
28
28
def sum(*values):
s = 0
for v in values:
s = s + v
return s
s = sum(1, 2, 3, 4, 5)
print(s)
15
def get_a(**values):
return values['a']
s = get_a(a=1, b=2) # returns 1
print(s)
1
And again, you can combine:
def sum(*values, **options):
s = 0
for i in values:
s = s + i
if "neg" in options:
if options["neg"]:
s = -s
return s
s = sum(1, 2, 3, 4, 5) # returns 15
s = sum(1, 2, 3, 4, 5, neg=True) # returns -15
s = sum(1, 2, 3, 4, 5, neg=False) # returns 15