Decoding Python’s *args and **kwargs function parameters | by Vishal Sharma | Towards Data Science

While using Python functions, you must have bumped into *args (args) or **kwargs (kwargs) parameters. In beginner definition, these functions let you write a function with a variable number or ’n’ number of arguments in Python.
Though it is the most layman definition to offer, these functions separately have specific purposes to serve. The actual syntax is * leading args while ** leading kwargs.
*args
If you pass *args as a parameter of the function, the function can now take more arguments than the number of defined arguments.
def myFun(arg1, arg2, arg3, arg4) :    print("arg1:", arg1)    print("arg2:", arg2)    print("arg3:", arg3)    print("arg4:", arg4) args = ("My", "name", "is","Vishal")myFun(*args)
The positional arguments are collected as a tuple. These parameters (arg1, arg2, arg3, arg4) in the function definition are taking values from the *args in a continuous manner.
def myFun(name, *args):    print(name)    if args:        print(args)myFun("Vishal") #name parameter passedmyFun("Vishal",1,2,3) #name and args parameter passed
When you pass your name in the first function call, it will return your name- in this case, “Vishal”.
When the function is called the second time, it passes the name argument along with three digits. These three digits are then stored in *args when the function is defined.
So, the output will look like
Vishal # For first function callVishal # For second function call(1,2,3)
Other functionality of args might come in handy in this way:
def fun(*args):    args=args + (4,)    return argsprint(fun(1,2,3))
After passing (1,2,3) in the args tuple, you can increase the size of the tuple using the below piece of code.
args=args + (4,)
As a result, you will see the output of the function like this:
(1,2,3,4)
**kwargs
Using **kwargs is almost the same as using *args but in **kwargs, you can pass in keyword arguments.
def myFun(**kwargs) :    for key, value in kwargs.items() :        print("%s == %s" % (key, value))myFun(first='Foo', second='Baz', third='Bar')
After running this function, the function will return both keys and values of kwargs.
first==Foosecond==Bazthird==Bar
Taking the example defined during *args explanation:
def myFun(name, **kwargs):    print(name)    if kwargs:        print(kwargs)myFun("Vishal") #name parameter passedmyFun("Vishal",one=1,two=2,three=3) #name and kwargs parameter passed
The first function call will only send “Vishal” to the name in definition, and “Vishal” will return.
In the second call, along with the name, keyword arguments will be stored in kwargs as a dictionary.
Vishal # For first function callVishal  # For second function{‘one’:1,’two’:2,’three’:3}
You can also use these parameters like:
def fun(**kwargs):    kwargs['name']='Vishal'    return kwargsfun(roll=12, subject='Data Science')
Now, this function will add one more key-value pair to the kwargs dictionary. And, the output will look something like this.
{'roll': 12, 'subject': 'Data Science', 'name': 'Vishal'}
Python surprises with its unique functionalities. And, using *args and **kwargs is something every beginner must know as they do come handy many times especially when you want to make your function flexible in terms of parameters.
For feedback and discussion, find me at my Linkedin!