Return values

The return values of functions are one of those things where Python is ahead of most other languages. Functions are usually allowed to return one object (one value) but, in Python, you can return a tuple, and this implies that you can return whatever you want. This feature allows a coder to write software that would be much harder to write in any other language, or certainly more tedious. We've already said that to return something from a function we need to use the return statement, followed by what we want to return. There can be as many return statements as needed in the body of a function.

On the other hand, if within the body of a function we don't return anything, or we invoke a bare return statement, the function will return None. This behavior is harmless and, even though I don't have the room here to go into detail explaining why Python was designed like this, let me just tell you that this feature allows for several interesting patterns, and confirms Python as a very consistent language.

I say it's harmless because you are never forced to collect the result of a function call. I'll show you what I mean with an example:

# return.none.py
def func():
pass
func() # the return of this call won't be collected. It's lost.
a = func() # the return of this one instead is collected into `a`
print(a) # prints: None

Note that the whole body of the function is composed only of the pass statement. As the official documentation tells us, pass is a null operation. When it is executed, nothing happens. It is useful as a placeholder when a statement is required syntactically, but no code needs to be executed. In other languages, we would probably just indicate that with a pair of curly brackets ({}), which define an empty scope, but in Python, a scope is defined by indenting code, therefore a statement such as pass is necessary.

Notice also that the first call of the func function returns a value (None) which we don't collect. As I said before, collecting the return value of a function call is not mandatory.

Now, that's good but not very interesting so, how about we write an interesting function? Remember that in Chapter 1, A Gentle Introduction to Python, we talked about the factorial of a function. Let's write our own here (for simplicity, I will assume the function is always called correctly with appropriate values so I won't sanity-check the input argument):

# return.single.value.py
def factorial(n):
if n in (0, 1):
return 1
result = n
for k in range(2, n):
result *= k
return result

f5 = factorial(5) # f5 = 120

Note that we have two points of return. If n is either 0 or 1 (in Python it's common to use the in type of check, as I did instead of the more verbose if n == 0 or n == 1:), we return 1. Otherwise, we perform the required calculation and we return result. Let's try to write this function a little bit more succinctly:

# return.single.value.2.py
from functools import reduce
from operator import mul

def factorial(n):
return reduce(mul, range(1, n + 1), 1)

f5 = factorial(5) # f5 = 120

I know what you're thinking: one line? Python is elegant, and concise! I think this function is readable even if you have never seen reduce or mul, but if you can't read it or understand it, set aside a few minutes and do some research on the Python documentation until its behavior is clear to you. Being able to look up functions in the documentation and understand code written by someone else is a task every developer needs to be able to perform, so take this as a challenge.

To this end, make sure you look up the help function, which proves quite helpful when exploring with the console.
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset