Decoding Python Functions: Default,  Positional, and Keyword Arguments
Photo by cottonbro studio: https://www.pexels.com/photo/photo-of-detective-decoding-characters-7319078/

Decoding Python Functions: Default, Positional, and Keyword Arguments

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.

No alt text provided for this image

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."

No alt text provided for this image

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:

No alt text provided for this image

*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.

No alt text provided for this image

**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.

No alt text provided for this image

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 share this story and?subscribe?to this newsletter if you are not yet a subscriber. You can also follow me on?LinkedIn.

.................................................................................................................................

In this modern era of AI, learning Python is the best investment that you can make for your future. If you are looking for a resource that will simplify the process of learning Python, please go to?Amazon?and get this Python book below.

No alt text provided for this image
Gregorio de Miguel Vadillo

Jefe de proyecto | Científico de datos | Máster Ciencia de datos

5 个月

Hi Benjamin, If I may, reading your article I found a little typo in the introduction. You refer to *agrs and **kwagrs when I guess you meant *args and **kwargs. Excellent article and thank you for sharing!

要查看或添加评论,请登录

社区洞察

其他会员也浏览了