Please help. Why do the arguments passed in the function automatically convert to a tuple? | Sololearn: Learn to code for FREE!

-1

Please help. Why do the arguments passed in the function automatically convert to a tuple?

def fun(*val): print(type(val)) one = [1,2,3,4,5] number = 400 fun(one,number)

10/19/2021 6:49:05 PM

Jenkins

7 Answers

New Answer

+4

Because the parameter "val" has the '*' in front of it in the function definition. it's usually set up like: def func(*args, **kwargs): ... *args being an arbitruary amount of arguments *kwargs being the same for dictionary values

+2

Yes and no, there are lots of ways it can be set up. And its a tuple because tuples can't be changed and that just makes logical sense. You put a bunch of values into a function so there's no way to change what you've already fed the function, hence, the tuple.

+1

Slick Thank you bro

0

Slick So ‘*’ automatically converts it to a tuple?

0

* in front of a parameter name converts all subsequent values given to an iterable which can then be looped through. It's a tuple because the arguments are given once and can't be changed. No sense in changing them

0

Slick Sorry, just to clarify the answer , so * in front of a parameter always converts values to a tuple? Yes? It can’t never be a str, list or anything else, always a tuple?

0

In normal unpackings like parallel assignments, it becomes a list. a, b, *c = 1, 2, 3, 4, 5 print(c) Only when inside a function as in your case, it becomes a tuple.