What does *args mean in Python?
Experience Level: Junior
Tags: Python
Answer
The asterisk before a method or function parameter name means that multiple arguments can be passed to the method/function.
Most frequently, *args is used, however, the name doesn't necessarily be only *args, but it can be *anythingElse.
Example:
def add(*numbers)
total = 0
for num in numbers:
total += num
return total
print(add(1, 5, 2)) # Displays 8
print(add(1)) # Displays 1
print(add(5, 2)) # Displays 7
Related Python job interview questions
How do you catch one specific exception in Python?
Python JuniorWhat does **kwargs mean in Python?
Python JuniorHow do you reraise an exception in Python?
Python JuniorWhy should you use private and protected methods instead of defining all methods as public in Python?
Python JuniorWhat is a static method in Python?
Python Junior