*Args and **Kwargs in Python
Let’s know about ** (dual star / asterisk) and * (star / asterisk) are operators in Python. These stars are used in Python for parameters. Args and kwargs are special symbols. These are used to give a variable number of args to function. Special symbol used for passing arguments are keyword arguments and non-keyword arguments. The * and ** are used to pass the no. of args in a function in Python.
Arguments passed as special symbols are:
- **kwargs (non-keyword arguments)
- *args (keyword arguments)
*Args in Python
The *Args is used to grant us to pass the number of non-keyword arguments to function and it is a special signature function in Python. *Args is used to pass variable length argument list and non-keyword.
- The signature is to use * to receive number of arguments.
- The *args is used to add more number of arguments
- If parameter is already used and we need to add extra arguments to that parameter with the help of *Args, we can do it.
- We can do multiply functions with the help of *args.
- Using the *, we can perform some high order functions such as map and filter.
Example for *Args
// Python program to demonstrate *args no of arguments with a variable.
defmyFun(*argv):
for i inargv:
print(i)
myFun ('Hey', 'Welcome', 'to', 'javatpoint')
Output:
Hey
Welcome
to
javatpoint
Example 2:
// Python program to demonstrate *args
defmyFun (arg1, *argv):
print ("First args:", arg1)
fori inargv:
print ("Next args through *argv:", i)
myFun('Hey', 'Welcome', 'to', 'javatpoint')
Output:
First args: Hey
Next args through *argv: Welcome
Next args through *argv: to
Next args through *argv: javatpoint
**kwargs in Python
*kwargs are used to pass variable length argument list and non-keyword. As we know kwasrgs was used with dual stars, it is used to pass through keyword args.
Example:
// Python program to demonstrate for a variable no. of keywords arguments.
defmyFun(**kwargs):
forkey, val inkwargs.items():
print("%s == %s"%(key, val))
// Driver code
myFun(first='java', mid='t', last='point')
Output:
first == java
mid == t
last == point
Example 2:
// Python program to demonstrate a variable number of keywords arguments with one more argument.
defmyFun(arg1, **kwargs):
forkey, val inkwargs.items():
print ("%s == %s"%(key, val))
// Driver code
myFun (‘Hey’, first='java', mid='t', last='point')
Output:
first == java
mid == t
last == point
Let’s use both **kwargs and *args to call a function with an argument.
// Python program to demonstrate **kwargs and *args as an argument in the demo function. So, here we are passing both variable length args and place of an argument which are having args.
defmyFun(arg5, arg6, arg7):
print("arg5:", arg5)
print("arg6:", arg6)
print("arg7:", arg7)
// Now we can use *args or **kwargs to pass args to this function:
args =("java", "t", "point")
myFun(*args)
kwargs ={"arg5": "java", "arg6": "t", "arg7": "point"}
myFun(**kwargs)
Output:
arg5: java
arg6: t
arg7: point
arg5: java
arg6: t
arg7: point
Example 2:
// Python program to demonstrate **kwargs and *args as an args in the demo function.
defmyFun(*args, **kwargs):
print("args: ", args)
print("kwargs: ", kwargs)
// Now we can use both *args, **kwargs to pass args to this function:
myFun ('java', 't', 'point', first="java", mid="t", last="point")
Output:
args: ('java', 't', 'point')
kwargs {'first': 'java', 'mid': 't', 'last': 'point'}