Decoding Python Functions: Default, Positional, and Keyword Arguments

Benjamin Bennett Alexander
CodeX
Published in
4 min readMay 18, 2023

--

Photo by cottonbro studio: https://www.pexels.com/photo/photo-of-detective-decoding-characters-7319078/

In this article, we are going to look at some of the most important features of Python functions. We will explore default, positional, and keyword arguments. This will help us understand the concept of using *agrs and **kwagrs as functional parameters, and how that helps us make our functions flexible and reusable.

Default Arguments

In Python, default arguments are the values that are automatically assigned to a function parameter if no argument is provided during the function call. This allows a function to be called with fewer arguments than specified. Default arguments are specified in the function definition and are typically used to provide a default value for an optional argument.

In the example below, we create a function with two parameters, and we pass a default argument to the “gender” parameter called “unknown.” When we call the function and we don’t pass any argument to the gender parameter, the function will use the argument specified in the function definition, which is “unknown.” When we call the function and we provide an argument for the gender parameter, the function will use the passed argument.

Positional Arguments (Non-Keyword Arguments)

In Python, when you call a function and provide arguments based on the position of the parameters, the arguments are called positional arguments. In the code below, we have created a function with two parameters: name and age. When we call the function, we provide arguments based on the order or position of the parameters. We pass the name John as the first argument because the name is the first parameter and age is the second parameter. So, basically, positional arguments are matched with parameters based on their position. Positional arguments are sometimes referred to as “non-keyword arguments.”

Keyword Arguments

Now, when we call the function and explicitly assign an argument to the parameter, the arguments are called keyword arguments. With keyword arguments, the order in which we pass the arguments does not matter since we explicitly assign the arguments to the variable. See the example below:

*args (non-keyword arguments)

In Python, when *args is passed as a function parameter, it means that the function can take an arbitrary number of positional arguments. The * before the parameter name in the function definition allows the function to accept any number of positional arguments, which will be wrapped up into a tuple. You can choose any name as a parameter name as long as you use * before the name. However, the convention is to use name args, and you should stick to that convention.

Below, we create a function that can take any number of positional arguments. We use the function to calculate the average of the numbers. Notice that when we call the function, the first time we pass three positional arguments, and the second time we pass six positional arguments. In both cases, our function returns the average of the arguments.

**kwargs (keyword arguments)

When we pass **kwargs as a function parameter, it means that our function can take any number of keyword arguments. The ** before the parameter name in the function definition allows the function to accept any number of keyword arguments. The arguments will be wrapped up in a dictionary. As per convention, you must use “kwargs” as the parameter name. Below, we create a function that can take any number of keyword arguments and calculate the average of the values. Notice that in the first call, we pass it two keyword arguments, and in the second call, it takes three keyword arguments.

Conclusion

Default arguments are the values that are automatically assigned to a function parameter if no argument is provided during the function call. Positional arguments are matched with parameters based on their position. When we call the function and explicitly assign an argument to the parameter, the arguments are called keyword arguments. In Python, you can use *args and **kwargs to create functions that can handle a variable number of arguments (positional and keyword arguments). These tools will make your code more flexible and reusable. Thank you for reading. Please clap, share this story, and subscribe if you are not yet a subscriber. You can also follow me on Twitter.

--

--

Benjamin Bennett Alexander
CodeX

Tech ( Python, Data science and Blockchain) and Finance enthusiast. I just share stories.