Now we have learned the basics of Python programming: variables, collections and loops. If you have followed the examples and the tutorial you might feel that sometimes we use too much code and it can be leveraged a bit. But maybe you do not know how to do it.
This article is an excerpt of my book Python 3 in Anger. For a more detailed version take a look at the book.
Most of the time the solution is to introduce functions. We have encountered functions of other modules which we used just like
isupper() of strings or
choice() of the
If you have learned mathematics then you know about functions already. Mathematicians can do very nasty things with them but programmers too. The aim of a function is to produce a result which is determined only by the parameters passed to the function.
In programming language we can look at a function as a black box where we send in a defined number of parameters (0 and more) and the function returns a result. In Python a function always returns a result even if you do not explicitly write or see a
return statement. This means if you know another programming language such as C++ or Java then you know about
void functions. In Python there is no such type — but we will see this later in this article when I tell you about return values.
Defining a function
A function is created with the
def statement. The general syntax looks like this:
<br /> def function_name(parameter list):<br /> function_body of statements<br />
The parameter list contains zero or more elements. If you call a function then you say arguments instead of parameters but this is terminology, in my eyes it is ok if you just say parameters even if you call a function. These parameters are mandatory if you do not declare them optional. We will take a look at optional parameters in the next section.
Every time a function is called the statements in its body are executed. Naturally you can use the
pass statement in the function’s body to do nothing — but in this case the
pass statement is executed too.
As you might know: a function has to have at least one statement in its body. Without it you get an error:
<br /> >>> def no_body_function():<br /> ...<br /> File "", line 2</p> <p> ^<br /> IndentationError: expected an indented block<br />
Well, this error message is not the most speaking but in this case the compiler is missing an indented block — at least one statement for the function’s body.
So let’s write a simple function which is an exchange calculator. It gets two parameters: the value and the exchange rate and it returns the changed value (value multiplied by exchange rate).
<br /> def exchange(value, rate):<br /> return value*rate<br />
So every time you define a function make sure you have an indented body. If you follow along this article in the interactive interpreter of Python the definition would look like this:
<br /> >>> def exchange(value, rate):<br /> ... return value*rate<br /> ...<br />
And as I mentioned previously you can have functions without return statements too. However most of the time you won’t use such functions but for the sake of brevity let’s see an example here too:
<br /> >>> def no_return():<br /> ... print("This function has no return statement")<br /> ...<br />
I think this is easy. You already know how to call functions but let’s quickly go through it. If you have a function definition then you can call it with passing the right arguments as parameters and you are good to go.
And as I told you before we already called functions. The very first and basic function we called was
print(). You can call it without any parameters and in this case it prints a new-line character to the output (an empty line). Alternatively we can pass an arbitrary number of parameters, each separated with a comma (
,) and they will be printed to the output too.
Now let’s call the two functions we defined in the previous section.
<br /> >>> no_return()<br /> This function has no return statement<br /> >>> exchange(123, 1.12)<br /> 137.76000000000002<br />
As you can see there is nothing complex in calling functions.
Previously I said that functions return values — even if you do not explicitly write a
return statement. Now it is time to verify my statement so I will show you that even the
no_return() function returns a value, and this value is
To see the return statement of the function let’s simply wrap the function call into a
print() function call.
<br /> >>> print(no_return())<br /> This function has no return statement<br /> None<br /> >>> print(exchange(123,1.12))<br /> 137.76000000000002<br />
Here you can see that even a function without
return statements returns a
None. This means in such cases you have to be careful how you use the return value because with a
None you can do almost nothing just use it in a boolean expression — with care of course.
<br /> >>> result_value = no_return()<br /> This function has no return statement<br /> >>> result_value + 5<br /> Traceback (most recent call last):<br /> File "", line 1, in<br /> TypeError: unsupported operand type(s) for +: 'NoneType' and 'int'</p> <p>>>> if result_value == False:<br /> ... print("Nothing useful...")<br /> ... else:<br /> ... print("Wow, we have got True back!")<br /> ...<br /> Wow, we have got True back!<br />
As you can see in the example above you cannot use
None in mathematical operations for example and
None is not
To fix the second part of the example we could change the code like this:
<br /> >>> if not result_value:<br /> ... print("Nothing useful...")<br /> ... else:<br /> ... print("Wow, we have got True back!")<br /> ...<br /> Nothing useful...<br />
The same goes for using only
return without any value. It results in the same
None after returning than no
return statement at all. Why should be this good? For example you want to terminate your function if a condition evaluates to true and want to return nothing. Naturally you can use
return None but the more pythonic solution would be to simply use
You can create functions with optional parameters. This means that these parameters do not have to be passed to the function. In this case their default value is used — and sometimes a block of statements is skipped if the optional parameter gets its default value.
Optional parameters have to follow the mandatory parameters and they have to have a default value. This value is used when you call the function and you do not provide this argument.
As an example let’s take the previously introduces
exchange function. As a quick reminder here is the definition:
<br /> def exchange(value, rate):<br /> return value * rate<br />
If we try to call this exchange function with only 1 parameter (with the
value) then we get an error from the interpreter:
<br /> >>> exchange(42)<br /> Traceback (most recent call last):<br /> File "", line 1, in<br /> TypeError: exchange() missing 1 required positional argument: 'rate'<br />
Now let’s make the
rate variable optional and set it’s default value to
1 to be able to call this function for the same currency without any exchange.
So the solution is to have a default value for the
rate parameter in the function and set this value to
To see how this works I changed the code a bit to display the current exchange rate too:
<br /> >>> def exchange(value, rate=1):<br /> ... print('Current exchange rate is', rate)<br /> ... return value * rate<br />
rate parameter is optional, we can call this function with or without a rate:
<br /> >>> exchange(124,0.78)<br /> Current exchange rate is 0.78<br /> 96.72<br /> >>> exchange(325,1)<br /> Current exchange rate is 1<br /> 325<br /> >>> exchange(42)<br /> Current exchange rate is 1<br /> 42<br />
Ordering of the optional and required parameters do matter. For example if we change the order and add
rate=1 as the first argument before
value we will get an error:
<br /> >>> def exchange(rate=1, value):<br /> ... return value * rate<br /> ...<br /> File "", line 1<br /> SyntaxError: non-default argument follows default argument<br />
If you think about it you will get the idea why it is this way: what if we provide one argument. Would it be the optional one or the required one? Well, the interpreter could not tell and maybe you would end up with the wrong result.
It can happen that you will encounter the term “keyword arguments” when learning Python. They are in fact the same as optional arguments: they have a name and a default value. And this name is the keyword and you can use it to assign a new value to this parameter.
Let’s look at the previous example again:
rate is the keyword argument of the function. Because
exchange has only one optional argument you can call it in two flavors with both parameters:
<br /> >>> exchange(42, 1.25)<br /> 52.5<br /> >>> exchange(42, rate=1.25)<br /> 52.5<br />
The second case is where we use keyword arguments.
Now let’s take another example where we have multiple optional parameters so you see how it really works with keyword arguments.
The example will be very basic: we define a function which takes four parameters
d and executes the following calculation:
a + b - c + d. And to have it work it requires only 2 parameters, two are optional.
<br /> >>> def sub_sum(a, b, c=0, d=0):<br /> ... return a + b - c + d<br /> ...<br /> >>> sub_sum(12,33)<br /> 45<br />
Now we can optionally pass values for the variables
d. If we already know that providing a value for
c has two flavors.
<br /> >>> sub_sum(12,33,0,10)<br /> 55<br /> >>> sub_sum(12,33,d=10)<br /> 55<br />
As you can see, you do not have to provide all the values, it is enough to assign the value for
d when calling the function. This is why they are called “keyword arguments”. And you might get the idea: there are functions which have a lot of arguments and most of the time you only need their default values. So you do not pass them along (so you do not have to know what the default value is) and you can fine-tune the function call with a single parameter which is somewhere in the list by using its keyword.
Taking this concept a bit further we can call functions with this keyword syntax in a way you cannot imagine in other languages: you are free to order the values as you want until you provide all the required arguments with their names.
<br /> >>> sub_sum(b=12,a=33,d=10)<br /> 55<br /> >>> sub_sum(d=10, 8, 11)<br /> File "", line 1<br /> SyntaxError: positional argument follows keyword argument<br />
As you can see you cannot omit the names of required arguments if you mess around with the order. In this case it is required to have them named to let the interpreter know that you want to set those values.
Traps with keyword arguments
Above we have seen one way of using keyword arguments. However every coin has two sides. Let’s dig a bit deeper when those default values are assigned. It is done when the function is created (so when the interpreter parses the function definition) and not when the function is invoked. This means we do not see any difference as long we use immutable types for named parameters / keyword arguments.
However problems can arise when we use mutable variables, for example a list:
<br /> >>> def append_if_short(s, lst=):<br /> ... if len(s) < 4:<br /> ... lst.append(s)<br /> ... return lst<br /> ...<br />
In the example above we append the parameter
s to the parameter
lst if the length of
s is at most 3. This seems fine if we pass both parameters to the function. But let’s call this function some times…
<br /> >>> append_if_short("one")<br /> ['one']<br /> >>> append_if_short("two")<br /> ['one', 'two']<br /> >>> append_if_short("three")<br /> ['one', 'two']<br /> >>> append_if_short("four")<br /> ['one', 'two']<br /> >>> append_if_short("five")<br /> ['one', 'two']<br /> >>> append_if_short("six")<br /> ['one', 'two', 'six']<br />
As you can see this results in an unexpected behavior. We pass in one string and get back a list with more elements than expected.
However this is not the full truth. We can make things ever worse:
<br /> >>> def append_if_short(s, lst=):<br /> ... if len(s) >> result = append_if_short("one")<br /> >>> result<br /> ['one']<br /> >>> result.append('five')<br /> >>> append_if_short('two')<br /> ['one', 'five', 'two']<br />
In the example above we have added an element to the list which us clearly longer than 3 characters — which again can lead to unexpected behavior.
To fix this let’s change the function definition:
<br /> >>> def append_if_short(s, lst=None):<br /> ... if lst is None:<br /> ... lst = <br /> ... if len(s) >> result = append_if_short("one")<br /> >>> result<br /> ['one']<br /> >>> result.append('five')<br /> >>> append_if_short('two')<br /> ['two']<br />
Sometimes (I hope every time) you will feel the urge to document your functions. This you can do with simple comments put around the function definition.
However there is a common practice which you should follow: docstrings. These are simple documentation strings placed right after the function definition. They have the special three quotation format because they are multiline strings describing your function.
Sometimes documentation is longer than the function itself. A convention is to make the first line of the docstring a brief one-line description, then have a blank line followed by a full description, and then some examples as they would appear if typed in the interactive interpreter.
So let’s use this guidance and add documentation to our
<br /> def append_if_short(s, lst=None):<br /> """ Returns a list containing s if the length of s is smaller than 4.</p> <p> s is any string, lst is an optional list-type parameter. If lst is not provided a new lst gets a new empty list assigned. If len(s) >> append_if_short('one')<br /> ['one']<br /> >>> append_if_short('two', ['one'])<br /> ['one', 'two']<br /> >>> append_if_short('three', )<br /> <br /> >>> append_if_short('four')<br /> <br /> """<br /> if lst is None:<br /> lst = <br /> if len(s) < 4:<br /> lst.append(s)<br /> return lst<br />