We will learn about Python *args and **kwargs, their purposes, and functions with examples in this post.
In programming, a function is used to create reusable code that performs similar operations. To carry out that action, we invoke a function with the specified value, which is referred to as a function argument in Python.
Positional vs keyword argument in Python:
Positional arguments are the types of arguments that are processed depending on their positional index when the function or method of interest is invoked. As a result, the order in which the positional arguments are presented is important since they are matched from left to right.
Consider the Python built-in function round(), which is used to round the input integer to the desired precision after the decimal point. The function is defined as round(number[, ndigits]), which implies that an input number is required, while the ndigits argument is optional and represents the desired accuracy.
Assume we have a float variable i.e. a = 10.2254 that we wish to limit to two decimal points of accuracy. We may do this by using the round() method and passing both parameters as positional arguments.
According to the function specification, because we gave positional arguments (i.e. we did not specify the keyword argument for each of the values provided in the caller), the first positional argument will correspond to number and the second to the option ndigits argument.
Keyword arguments, on the other hand, are supplied to a function by providing the keyword in the form name=value. As a result, the order in which these arguments are given in the caller is irrelevant because they are matched by argument name.
Returning to the round() function example, we may call it by giving keyword parameters.
And, as previously stated, the sequence in which we submit keyword arguments is unimportant:
It's worth noting that we could even mix positional and keyword arguments, with the latter coming after the former —
However, if you use a keyword argument before a positional argument, a SyntaxError will be thrown.
Now since you have learnt about Positional and Keyword arguments, let us check their application with *args and **kwargs
Positional arguments and *args
Assume we wish to construct a Python function that accepts an arbitrary number of parameters. One approach is to send the arguments as a collection, such as a list, although this will be inconvenient in most circumstances. Furthermore, this concept isn't entirely Pythonic — but that's where the arbitrary arguments list comes in.
Instead, we might use the *args idiom when declaring our function to declare that it may genuinely accept any number of arguments. And it is up to the implementation to appropriately handle the specified parameters.
The unpacking operator is represented by the star *, and it returns a tuple containing all of the parameters given by the caller.
We can now call the aforementioned method with whatever amount of parameters we want. This approach uses Positional arguments concept.
Keyword argument and **kwargs:
Similarly, we may wish to construct functions that may receive an arbitrary number of keyword arguments from time to time.
When the keyword arguments from ** are unpacked, the output is a Python dictionary with keys corresponding to keyword names and values corresponding to the actual argument values supplied.
And now we may use as many keyword parameters as we like to invoke the function.
In this post, we learnt about two Python special keywords: *args and **kwargs. These make a Python function more adaptable, allowing it to accept a variable amount of parameters and keyword arguments.
This approach uses Keyword arguments concept.
Keep Rocking! Stay Connected with Padhai Time!
We collect cookies and may share with 3rd party vendors for analytics, advertising and to enhance your experience. You can read more about our cookie policy by clicking on the 'Learn More' Button. By Clicking 'Accept', you agree to use our cookie technology.
Our Privacy policy can be found by clicking here