본문 바로가기

Language/Python

[Syntax] Special arguments *args and **kwargs

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}