- 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)
6 Answers
+ 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?