Returning Values From Functions

PythonPythonBeginner
Practice Now

This tutorial is from open-source community. Access the source code

Introduction

Objectives:

  • Returning values from functions

In this exercise, we briefly look at problems related to returning values from functions. At first glance, it seems like this should be straightforward, but there are some subtle problems that arise.


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL python(("`Python`")) -.-> python/BasicConceptsGroup(["`Basic Concepts`"]) python(("`Python`")) -.-> python/DataStructuresGroup(["`Data Structures`"]) python(("`Python`")) -.-> python/FunctionsGroup(["`Functions`"]) python(("`Python`")) -.-> python/ModulesandPackagesGroup(["`Modules and Packages`"]) python(("`Python`")) -.-> python/AdvancedTopicsGroup(["`Advanced Topics`"]) python/BasicConceptsGroup -.-> python/comments("`Comments`") python/DataStructuresGroup -.-> python/tuples("`Tuples`") python/FunctionsGroup -.-> python/function_definition("`Function Definition`") python/ModulesandPackagesGroup -.-> python/importing_modules("`Importing Modules`") python/ModulesandPackagesGroup -.-> python/using_packages("`Using Packages`") python/ModulesandPackagesGroup -.-> python/standard_libraries("`Common Standard Libraries`") python/AdvancedTopicsGroup -.-> python/decorators("`Decorators`") python/AdvancedTopicsGroup -.-> python/threading_multiprocessing("`Multithreading and Multiprocessing`") python/BasicConceptsGroup -.-> python/python_shell("`Python Shell`") python/FunctionsGroup -.-> python/build_in_functions("`Build-in Functions`") subgraph Lab Skills python/comments -.-> lab-132504{{"`Returning Values From Functions`"}} python/tuples -.-> lab-132504{{"`Returning Values From Functions`"}} python/function_definition -.-> lab-132504{{"`Returning Values From Functions`"}} python/importing_modules -.-> lab-132504{{"`Returning Values From Functions`"}} python/using_packages -.-> lab-132504{{"`Returning Values From Functions`"}} python/standard_libraries -.-> lab-132504{{"`Returning Values From Functions`"}} python/decorators -.-> lab-132504{{"`Returning Values From Functions`"}} python/threading_multiprocessing -.-> lab-132504{{"`Returning Values From Functions`"}} python/python_shell -.-> lab-132504{{"`Returning Values From Functions`"}} python/build_in_functions -.-> lab-132504{{"`Returning Values From Functions`"}} end

Returning Multiple Values

Suppose you were writing code to parse configuration files consisting of lines like this:

name=value

Write a function parse_line(line) that takes such a line and returns both the associated name and value. The common convention for returning multiple values is to return them in a tuple. For example:

>>> parse_line('email=guido@python.org')
('email', 'guido@python.org')
>>> name, val = parse_line('email=guido@python.org')
>>> name
'email'
>>> val
'guido@python.org'
>>>

Returning Optional Values

Sometimes a function might return an optional value--possibly as a mechanism for indicating success or failure. The most common convention is to use None as a representation for a missing value. Modify the parse_line() function above so that it either returns a tuple on success or None on bad data. For example:

>>> parse_line('email=guido@python.org')
('email', 'guido@python.org')
>>> parse_line('spam')       ## Returns None
>>>

Design discussion: Would it be better for the parse_line() function to raise an exception on malformed data?

Futures

Sometimes Python code executes concurrently via threads or processes. To illustrate, try this example:

>>> import time
>>> def worker(x, y):
        print('About to work')
        time.sleep(20)
        print('Done')
        return x + y

>>> worker(2, 3)     ## Normal function call
About to work
Done
5
>>>

Now, launch worker() into a separate thread:

>>> import threading
>>> t = threading.Thread(target=worker, args=(2, 3))
>>> t.start()
About to work
>>>
Done

Carefully notice that the result of the calculation appears nowhere. Not only that, you don't even know when it's going to be completed. There is a certain coordination problem here. The convention for handling this case is to wrap the result of a function in a Future. A Future represents a future result. Here's how it works:

>>> from concurrent.futures import Future
>>> ## Wrapper around the function to use a future
>>> def do_work(x, y, fut):
        fut.set_result(worker(x,y))

>>> fut = Future()
>>> t = threading.Thread(target=do_work, args=(2, 3, fut))
>>> t.start()
About to work
>>> result = fut.result()
Done
>>> result
5
>>>

You'll see this kind of pattern a lot of if working with thread pools, processes, and other constructs. For example:

>>> from concurrent.futures import ThreadPoolExecutor
>>> pool = ThreadPoolExecutor()
>>> fut = pool.submit(worker, 2, 3)
About to work
>>> fut
<Future at 0x102157080 state=running>
>>> fut.result()
Done
5
>>>

Summary

Congratulations! You have completed the Returning Values From Functions lab. You can practice more labs in LabEx to improve your skills.

Other Python Tutorials you may like