My understanding is that *args
can be used to allow an arbitrary number of inputs in a Python function. For example, the function
def f(*x):
for v in x:
print(v)
could be called as
f(1)
# or
f(1, 2, 3)
However, it seems to me that this is equivalent to defining a function that takes a list of values as an argument. For example, we could define the same function as
def f(x):
for v in x:
print(v)
and call it as
f([1])
# or
f([1, 2, 3])
and this would result in the same behavior.
Given this, why should we use the *
pattern in defining functions instead of passing lists? Is it more "correct"? Faster? More secure? Or are there use cases where only the *
pattern achieves the desired outcome?