There are several answers to what Python's splat operator does (unpack lists or tuples into separate args inside the called function) but I can't find anything about the interesting side effect of how the splat operator coerces lists to tuples (effectively rendering passing a list with splat as being passed by value, not reference).
So, is it true that Python's splat operator coerces lists into tuples:
def test(*arg):
print(type(arg))
for a in arg:
print(a)
lst = ['roger', 'colleen']
print(type(lst))
test(*lst)
tpl = ('roger', 'colleen')
print(type(tpl))
test(*tpl)
It seems to. The code above produces this output:
<class 'list'>
<class 'tuple'>
roger
colleen
<class 'tuple'>
<class 'tuple'>
roger
colleen
I'm trying to solidify my knowledge of lists and tuples. I read here that
The differences between tuples and lists are, the tuples cannot be changed unlike lists and tuples use parentheses, whereas lists use square brackets.
I was at first surprised that calling test() with a list didn't let test() change the list (they aren't immutable). But they are when passed with the splat operator to function because they get coerced to a tuple. Right? I think!
Update: rkersh's comments said something that adds a lot of clarity:
test(*arg) isn't being called with a list, it's being called with the items in the list)--and those items are seen by *arg as a tuple.