0
Doubt in the function argument in python
What is the difference between a and *a in the argument variable? I have seen this many a times but couldn't understand it. Can anyone explain in detail?
5 Answers
+ 2
I made good example
def log(*a):
print(*a, file=sys.stderr)
log(2,"banana")
log(2,4,10,20)
Operator * allows to call method with as many parameters as you want without wrapping them into array or tuple like this log((2,4,10,20))
Python does it for you
+ 2
def myfunct(*args):
for x in args:
print(x)
one = 1
two = 2
three = 3
four = 4
myfunct(one, two, three, four)
five = 5
myfunct(one, two, three, four, five)
it scoops up all the function arguments into a tuple so you can then iterate over it..
0
Thank you
0
Try to run my example without one of * for better understanding
0
Ok