May 2019
Beginner
528 pages
29h 51m
English
In the last chapter, we mentioned that all objects are passed by reference and demonstrated passing an immutable object as a function argument. Here, we discuss references further by examining what happens when a program passes a mutable list object to a function.
Consider the function modify_elements, which receives a reference to a list and multiplies each of the list’s element values by 2:
In [1]: def modify_elements(items):...: """"Multiplies all element values in items by 2."""...: for i in range(len(items)):...: items[i] *= 2...:In [2]: numbers = [10, 3, 7, 1, 9]In [3]: modify_elements(numbers)In [4]: numbersOut[4]: [20, 6, 14, 2, 18]
Function modify_elements ...
Read now
Unlock full access