What does **kwargs mean in Python?
Experience Level: Junior
Tags: Python
Answer
The double asterisk before a method or function parameter name means that multiple keyword arguments can be passed to the method/function.
Keyword argument is a key-value pair, the type of the **kwargs is dictionary.
Most frequently, name **kwargs is used, however, the name doesn't necessarily be only **kwargs, but it can be **anythingElse.
Example:
def sum(**kwargs)
total = 0
print(type(kwargs)) # Displays <class 'dict'>
for value in kwargs.values():
total += value
return sum(red=1, amber=2, green=3)
Related Python job interview questions
How do you catch multiple different exceptions in Python and handle each of them differently?
Python JuniorHow do you catch one specific exception in Python?
Python JuniorWhat does *args 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 Junior