What does *args and **kwargs mean?

Last updated 3 weeks, 5 days ago | 26 views 75     5

Tags:- Python

Python | *args and **kwargs 

  • *args is a special syntax used in the function definition to pass variable-length arguments. e.g: myFun(*argv)
    def func(*args):
    	for i in args:
    		print(i)
    
    func(2,3,4)
    
    
    #Output
    2
    3
    4

     

  • **kwargs is a special syntax used in the function definition to pass variable-length keyworded arguments. e.g: myFun(**kwargs)
    def func(**kwargs):
    	for i in kwargs:
    		print(i, kwargs[i])
    
    func(a=1,b=2,c=3)
    
    
    #Output:
    a 1
    b 2
    c 7

The words args and kwargs are a convention, and we can use anything in their place.