In Python, the special syntax *args and **kargs are used to pass a variable number of arguments to a function.
*args is used to pass a non-keyworded, variable-length argument list to a function as a Tuple (Data Structure in Python).
**kargs is used to pass a keyworded, variable-length argument list to a function as a Dictionary (Data Structure in Python).
You can name *args and **kargs anything you like such as *myargs and **mykargs. However, it’s better to follow the convention followed by the python community (i.e. *args and **kargs).
In this article I will explain the special terms *args and **kargs in Python and their usage.
Function with Normal argument
Look at the below function which has a normal positional argument. If you want to call this function you always have to pass one argument only, anything else you will get an error.
def normal_args_function(args):
print (args)
# Call the function
normal_args_function(“hello args”)
# Output:
hello args
Here comes the magic of *args and **kargs to handle a variable number of argument list in a function call.
Function with *Args (special argument)
Look at the below function which has a *args argument. Now you can call this function with a variable number of non-keyworded arguments.
def special_args_function(*args):
print (args)
# Call the function with no argument
special_args_function()
# Output: Empty Tuple as nothing is passed
()
# Call the function with one argument
special_args_function("hello *args")
# Output: Tuple with one item
('hello *args',)
# Call the function with two arguments
special_args_function("hello *args", "special argument")
# Output: Tuple with two items
('hello *args', 'special argument')
Function with **Kargs (special argument)
Look at the below function which has a **kargs argument. Now you can call this function with a variable number of keyworded arguments.
def special_kargs_function(**kargs):
print (kargs)
# Call the function with no argument
special_kargs_function()
# Output: Empty Dictionary as nothing is passed
{}
# Call the function with one argument
special_kargs_function(greeting="hello *kargs")
# Output: Dictionary with one item
{'greeting': 'hello *kargs'}
# Call the function with two arguments
special_kargs_function(greeting="hello *kargs", type="special argument")
# Output: Dictionary with two items
{'greeting': 'hello *kargs', 'type': 'special argument'}
As you already know **kargs contains a dictionary, so If you want to retrieve the the key/value pair of the dictionary you can use the items() method. Look at the example below.
def special_kargs_function(**kargs):
for key, value in kargs.items():
print("{}: {}".format(key, value))
# Call the function with one argument
special_kargs_function(greeting="hello *kargs")
# Output:
greeting: hello *kargs
# Call the function with two arguments
special_kargs_function(greeting="hello *kargs", type="special argument")
# Output:
greeting: hello *kargstype: special argument
Function with *Args and **Kargs together
Remember that, *args comes before **kargs while used together in a function. Look at the below example.
def special_args_and_kargs_function(*args, **kargs):
print(args)
print(kargs)
# Call the function with no argument
special_args_and_kargs_function()
# Output:
(){}
# Call the function with one argument for *args, and one for **kargs
special_args_and_kargs_function("hello *args", greeting="hello *kargs")
# Output:
('hello *args',){'greeting': 'hello *kargs'}
# Call the function with one argument for *args, and two arguments for **kargs
special_args_and_kargs_function("hello *args", greeting="hello *kargs", type="special argument")
# Output:
('hello *args',){'greeting': 'hello *kargs', 'type': 'special argument'}
Function with Normal and Special arguments together
Remember that, if you use *args and **kargs together with one or more normal arguments then always put the *args and **kargs at the end of your normal argument list. Look at the below function.
def normal_and_special_arguments_function (normal_args, *args, **kargs):
print (normal_args)
print (args)
print (kargs)
# Call the function
normal_and_special_arguments_function("hello normal args", "hello *args", greeting="hello *kargs")
# Output:
hello normal args('hello *args',){'greeting': 'hello *kargs'}
Summary
For your convenience, following are some key points to remember while dealing with *args and **kargs.
- *args and **kargs are special syntax in python to pass a variable number of arguments to a function.
- *args contains non-keyworded, variable-length argument list as a Tuple.
- **kargs contains keyworded, variable-length argument list as a Dictionary.
- *args comes before **kargs while used together in a function.
- normal argument list positioned before *args and **kargs while used together in a function.
Happy Python coding!
Comments
You are welcome to write comments, suggestions, corrections, or any queries related to the article. Your comments may take some time to be appeared. Please be aware that any irrelevant comments will be deleted. Thanks for your understanding, and your respectful & relevant comments!