In this post, master the essentials of Python functions and methods, from defining parameters and arguments to leveraging optional and keyword-only parameters, with practical examples and insights.
A function in Python programming is a named subroutine that encapsulates a commonly needed functionality and can be executed via a function call. An example of a function is the max built-in function for determining the largest element of a list:
>>> max([3,6,2,1,9])
9
The list whose largest element is to be determined is a parameter of the max function and is written into the parentheses when the function is called.
If a function call returns a result, this return value can be used as a normal instance:
>>> value= max([3,6,2,1,9])
>>> value/2
4.5
A method is a function that’s executed in the context of a specific instance. For example, lists have a sort method that sorts the list for which it is called:
>>>list=[4,6,2,1,8,5,9]
>>> list.sort()
>>> list
[1, 2, 4, 5, 6, 8, 9]
The data type of an instance determines which methods are available to it.
Note: Below we will talk in a little more detail about the different possibilities for function parameters in Python. Note that the literature distinguishes between two terms, which are based on different views of a function call: parameter and argument.
While the term argument refers to a concrete value passed in a function call, the corresponding placeholder in the function definition is called a parameter.
It is important to be aware of this slight difference in meaning. However, to improve readability, we have decided against making a strict distinction between these meanings and use the terms parameter and argument almost synonymously.
Positional Parameters
Many methods need additional information besides the instance that the call references in order to work. For this purpose, so-called parameters are available, which are written, separated by commas, in the brackets at the end of the method call. Both references and literals can be specified as parameters:
var = 12
reference.method(var, "Hello World!")
The definition of a method determines how many and which parameters may be passed to it, which therefore varies from method to method.
Taken together, a method and the parameters it expects is referred to as an interface. Every time we describe a method in this book, we usually specify the associated interface in the following form:
method(parameter1, parameter2, parameter3)
In this case, the method expects three positional parameters named parameter1, parameter2, and parameter3. The term positional means that the instances passed in the method call are assigned to the parameters according to their position in the parameters list. In the case of the method call reference.method(1, 45, -7), parameter1 has the value 1, parameter2 the value 45, and parameter3 the value -7.
Keyword Arguments
You can also pass keyword arguments to a method. Keyword arguments are directly linked to the formal parameter name, and their order in the parameters list no longer matters. To pass a value as a keyword argument, you must assign the value to be passed to the parameter name within the method call using the equal sign. Accordingly, the following two method calls are equivalent:
reference.method(1, 2, 3)
reference.method(parameter2=2, parameter1=1, parameter3=3)
You can also mix positional arguments and keyword arguments, but all keyword arguments must be located at the end of the argument list. Thus, the following call is equivalent to the previous two:
reference.method(1, parameter3=3, parameter2=2)
Only parameter1 was passed as a positional argument, while parameter2 and parameter3 were passed as keyword arguments.
It’s up to you which of the two transfer methods you want to use. Usually, however, parameters are passed as positional arguments because of the lower effort required for writing them.
Optional Parameters
There are optional parameters available, which must be passed only if needed. When we introduce methods with such parameters, they are indicated by square brackets in the interface description:
method(parameter1, [parameter2, parameter3])
In this example, parameter1 is a required parameter, while parameter2 and parameter3 are two optional parameters. So the method can be called in different ways:
reference.method(1, 2, 3)
reference.method(1, 2)
reference.method(1)
Keyword-Only Parameters
A function or method can have keyword-only parameters. These are parameters that can only be passed in keyword notation. We’ll indicate these parameters in a function or method interface by curly braces:
method(parameter1, parameter2, {parameter3})
Keyword-only parameters can be optional or nonoptional. The method can thus be called as follows:
reference.method(1, 2, parameter3=3)
reference.method(1, 2)
Editor’s note: This post has been adapted from a section of the book Python 3: The Comprehensive Guide by Johannes Ernesti and Peter Kaiser.
Comments