Python functions, Parameters, Arguments, args and kwargs

Python functions

A function is a construct that helps us perform some action using a block of code (the body of the function), sometime based on input parameters. These functions can take different forms and can do a lot to allow your functional code base to be effective,

To define a function, you use the def keyword followed by the name of the function and the parenthesis in which you add the comma-separated list of parameters. If there is no parameter, then the name of the function is simply followed by ().

The following is valid function definition:

Here the body of the function is the comment we have and the pass statement, which means the function, is doing nothing.

Note that there are two other ways, the function could be called. First, the parameters can be passed in any order, as long as they are mentioned explicitly in the call, as in sum_numbers(prm2 = 10, prm1 = 5). Second, it is possible to call the function without mentioning the parameters, as long as you pass them in same order as in the function’s definition as in sum_numbers(5, 10).

Returning a value

A function either returns a value or returns None, a special value that contains nothing. By the way, None is similar to the integer 0 or the empty string or the empty list.

Let’s first discuss the case where a function returns None. As an example of this we can take the previous function again, sum_numbers(), and adjust it a bit. Let’s see an example:

Do you see what is going on here? There is nothing in this function’s body that returns a value, so there is nothing returned, and that’s why the res variable (in res = sum_numbers(prm1 = 5, prm2 = 10)) evaluates to None.

A simple change in the code will fix the situation. We have to explicitly return the value obtained by sum, using the return keyword. Here is the new version:

When executing the code, using python we get the result:15. This is how function returns a value (that is not None).

By the way, in Python, nothing stops us from explicitly returning None, if we want. So, for example (although you would probably not have a function that only does this), the following code in valid:

This implicit effect without the return statement is that the function returns None. What we add is that we make it explicit, so as our code example to handle complicated use cases, it is easy to reason about it. Also, with complex code, such little things will help someone else quickly understand what the code is doing and may save time when debugging.

Parameters and arguments

As we just saw that you use parameters too provide input for a function when calling it. We talk about “passing arguments to the parameters”. The result is that the function produces a different effect depending on the arguments.

A function can be defined without parameters, and that happens a lot. But a function with parameters gives us more power since we can reuse it in many more situations. Of course, with more power comes more responsibility: we have to be more careful with the code we write, to handle the possible input types and values, avoid bugs and do more testing.

Default values

One interesting thing is that we can provide default values for parameters.

In our previous example, we can use a default value of 1 for the nb parameters, as follows:

Documenting functions

It is recommended to write documentation for our code. In python, we provide inline documentation by adding documentation strings to the functions we write. A documentation string is a text, enclosed in triple quotes, just before the code of a function body starts. So, taking the previously features function, again, adding a documentation string would look as follows (partially showing the function’s definition):

Once a function has a docstring, we can access this documentation using the help() function. So let’s see whatwe get with the function definition and a call where we pass the reference of the function to help() and we print the returned result. The complete code is as follow:

*args and **kwargs

In some cases you want to create a function what a variable number of parameters. For example, we start defining a function as follow:

Using this form gives a signature a bit long and boring. Also, as you can see, apart from firstname and lastname, some parameters have a default value of “”, because we are not going to need them in all cases of calling the function.

That’s where we can use *args as special parameters whose purpose is to allow a non-keyworded variable length arguments list (including the case of no argument at all). Now, that has an impact on the way we write the code in the function body.

So, our basic example code would look like the following (using the Python interpreter):

That helps show that the technique is handy and provides a  way to organize function code in another style by declaring less parameters explicitly; we can group a bunch of parameters together using this *args variable unpacking mechanism.

One thing you notice in the example is that we do not have control over which value matches which parameter. No worry, in that case, we have the alternative technique: the keyworded variable length arguments list. This mechanism allows using named arguments when calling the function, if we define the function using **kwargs as a special parameter.

Let’s see things in action with our simple example, as follows: Let’s see things in action with our simple example, as follows:

Let’s see another example:

we are passing args and *kwargs as an argument in the my_function. By passing argstomy_functionsimply means that we pass the positional and variable-length arguments which are contained by args. so, “Hello” pass to the arg1, “coders” pass to the arg2. when we pass *kwargs as an argument to the my_function it means that it accepts keyword arguments. Here, “arg1 is key and the value is “Hello” which is passed to arge1, and just like that “coders” pass to arg2. After passing all the data we are printing all the data in lines.

This is how you can use **kwargs, when needed.

Note that you can use both the *args technique and **kwargs one in the same function, and this is something commonly done by seasoned developers.

Popular Posts

Author

Spread the knowledge
 
  

Leave a Reply

Your email address will not be published. Required fields are marked *