Types of Parameters in a Python Function

In this tutorial I want to teach you something that I consider essential for you to control the Python language: The types of parameters of a function. If you have been playing with the language, you will know that when defining a function you can indicate a series of parameters.

In the following example, the is_greater function returns whether the parameter x is greater than the parameter y:

def is_greater(x, y):
    return x > y

When calling the function, we will do it as follows:

>>> is_greater(5, 3)
True

However, if when calling the function we don’t pass all the arguments, the interpreter will throw an exception:

>>> is_greater ( 5 )
Traceback ( most recent call last ) : 
  File "<input>" , line 1 , in < module > 
TypeError: is_greater () missing 1 required positional argument: 'y'

What it is telling us is that the positional argument ‘ y’ is required and has not been specified.

Before continuing, it is important to note that, by default, the values ​​of the arguments are assigned to the parameters in the same order in which they are passed when calling the function. Later we will see that this circumstance can change.

Optional parameters in a Python function

In addition to what we have seen so far, a series of optional parameters can be indicated in a Python function . They are parameters that are indicated with a default value and if they are not passed when calling the function then they take this value.

Let’s imagine the constructor (method __init__ ) of a Point class that takes the values ​​of the coordinates in which an object of that class will be created:

class Point:
    def __init__ ( self, x, y ) : 
        self.x = x
        self.y = y
    def __repr__ ( self ) : 
        return "x: {}, y: {}" . format ( self.x, self.y )

As we can see, two parameters are indicated in the __init__ method: the x coordinate and the y coordinate of a point:

>>> Point ( 1 , 2 )
x: 1 , y: 2

As I indicated in the previous section, if we call the function without passing any argument, our code will fail:

>>> dot ()
Traceback ( most recent call last ) : 
  File "<input>" , line 1 , in < module > 
TypeError: __init__ () missing 2 required positional arguments: 'x' and 'y'  

We can enhance our __init__ function so that if any of the parameters are not specified, then it defaults to 0. To optionally specify a parameter, use the ‘ = ‘ operator. Let’s see how it would look:

def __init__ ( self, x= 0 , y= 0 ) : 
    self.x = x
    self.y = y

Now we can call the function as follows:

>>> point ()
x: 0 , y: 0

>>> Point ( 3 )
x: 3 , y: 0

Consider next the following greeting function:

def greeting ( name, message = "hey this is tapan saha" ) : 
    print ( "Hello {}, {}" . format ( name, message ))

The name parameter does not indicate a default value, therefore, it is mandatory. The same is not true for the message parameter , whose default value is “hey this is tapan saha “. If this argument is not passed, that value will be taken by default. On the contrary, if it is indicated, it will be overwritten with the new value.

You can specify as many optional parameters as you want in a function. However, once one is specified, all parameters to its right must also be optional. This means that mandatory parameters cannot follow default parameters.

The following example defines a function incorrectly:

def greeting ( message= "hey this is tapan saha" , name )

When trying to make use of the function, the Python interpreter will indicate the error:

def greeting ( message= "hey this is tapan saha" , name ) : 
              ^
SyntaxError: non-default argument follows default argument

Positional Parameters and Named Parameters in a function

As I told you before, when we call a function in Python with different arguments, the values ​​are assigned to the parameters in the same order in which they are indicated.

Continuing with the previous example of the greeting function, let’s imagine that we call the function as follows  greeting ( “yourswriter” , ” welcome to my programming blog )In this way, the name parameter will be assigned the value “j2logo” and the “ message ” parameter the value “how are you?”

This is so because the value at which the arguments are assigned depends on the order in which they are called . However, the order can be changed if we call the function indicating the name of the parameters. To make it clearer, the following examples are all valid:

>>> greeting ( message= "welcome to my programming blog" , name= "yourswriter" )
Hello yourswriter, welcome to my programming blog

>>> greeting ( name= "yourswriter" , message= "welcome to my programming blog" )
Hello yourswriter, welcome to my programming blog

>>> greeting ( "yourswriter" , message= "welcome to my programming blog" )
Hello yourswriter, welcome to my programming blog

As we can see, we can mix the order of the parameters if we indicate their name. Of course, they must always be to the right of the positional parameters, that is, those that are indicated without a name and whose value is assigned in the order in which it is indicated.

Variable number parameters: *args and **kwargs in Python.

So far I’ve taught you the basics about the different types of parameters when defining a Python function. However, if you want to be a true pythonist 🐍and complete your training, you should read the following tutorial to understand ☞ what the *args and **kwargs parameters mean in a function. By following this tutorial, you will have completed your training on parameter types in a Python function.

Conclution

Here are the key points that can be extracted from this tutorial:

  • By default, when calling a function, the values ​​of the arguments are assigned in the same order in which they are passed when calling that function.
  • Optional parameters are indicated with the ‘=’ operator, have a default value, and are always defined after mandatory parameters.
  • You can modify the order of the arguments with which a function is called by indicating the name of the parameters. Named parameters always appear after positionals.

I hope it has been helpful for you 😉