- 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)

19th Oct 2021, 6:49 PM
Jenkins
6 Réponses
+ 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
19th Oct 2021, 6:53 PM
Slick
Slick - avatar
+ 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.
19th Oct 2021, 7:44 PM
Slick
Slick - avatar
+ 1
Slick Thank you bro
19th Oct 2021, 7:33 PM
Jenkins
0
Slick So ‘*’ automatically converts it to a tuple?
19th Oct 2021, 7:33 PM
Jenkins
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
19th Oct 2021, 7:36 PM
Slick
Slick - avatar
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?
19th Oct 2021, 7:41 PM
Jenkins