1. What is *args and **kwargs arguments?
*args allows us to pass a variable number of non-keyword arguments to a Python function. In the function, we should use an atserisk(*) before the parameter name to pass a variable number of arguments.Thus, we're sure that these passed arguments make a tuple inside the function with the same name as the parameter excluding *.
**kwargs allows us to pass a variable naumber of keywords arguments to a Python function. In the function, we use double asterisk(*) before the parameter name to denote this type of arguments.
Thus, the arguments are passed as a dictionary and these arguments make a dictionary inside the function with name same as the parameter excluding **.
def student_info(*args, **kwargs):
print(args)
print(kwargs)
student_info('Math', 'Art', name='Jon', age=22)
# Result of function
# ('Math', 'Art')
# {'name' : 'John', 'age' : 22}
'Language > Python' 카테고리의 다른 글
[Error] pipenv - ModuleNotFoundError (0) | 2022.10.17 |
---|---|
[unittest] Unit Testing Our Code (0) | 2022.10.16 |
[Error] Why does substring slicing with index out of range work? (1) | 2022.10.13 |
[os] User Underlying Operating System (1) | 2022.10.03 |
[sys] The way to import module not found (0) | 2022.10.03 |