I am fairly new to Python3. I have a question with Variable Length Arguments(*args). Based on my understanding the reason why we use the Variable Length Arguments list(or tuple) is that they are useful when you need a function that may have different numbers of arguments. A function like this
def main():
x = ('meow', 'grrr', 'purr')
kitten(*x)
def kitten(*args):
if len(args):
for s in args:
print(s)
else: print('Empty')
if __name__ == '__main__': main()
gives the same output as
def main():
x = ('meow', 'grrr', 'purr')
kitten(x)
def kitten(args):
if len(args):
for s in args:
print(s)
else: print('Empty')
if __name__ == '__main__': main()
output
meow
grrr
purr
I don't see the difference, and why is it better to use Variable Length Arguments(*args). Could someone please explain this to me?
And also, what does the asterisk really do?
x = ('meow', 'grrr', 'purr')
print (*x)
output
meow grrr purr
seems like, it just takes the variables inside the tuple out. And if I do
print (len(*x))
print (type(*x))
it will give me error
print (len(*x))
TypeError: len() takes exactly one argument (3 given)
print(type(*x))
TypeError: type.__new__() argument 2 must be tuple, not str