def foo(a, b, c):
print a+b+c
i = [1,2,3]
Is there a way to call foo(i) without explicit indexing on i? Trying to avoid foo(i[0], i[1], i[2])
Yes, use foo(*i)
:
>>> foo(*i)
6
You can also use *
in function definition:
def foo(*vargs)
puts all non-keyword arguments into a tuple called vargs
.
and the use of **
, for eg., def foo(**kargs)
, will put all keyword arguments into a dictionary called kargs
:
>>> def foo(*vargs, **kargs):
print vargs
print kargs
>>> foo(1, 2, 3, a="A", b="B")
(1, 2, 3)
{'a': 'A', 'b': 'B'}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With