1 Mutation
Let’s imagine you order a mushroom and cheese pizza from La Val’s, and that they represent your order as a list:
1 | >>> pizza = ['cheese', mushrooms'] |
A couple minutes later, you realize that you really want onions on the pizza. Based on what we know so far, La Val’s would have to build an entirely new list to add onions:
1 | >>> pizza = ['cheese', mushrooms'] |
This is silly, considering that all La Val’s had to do was add onions on top of pizza instead of making an entirely new pizza.
We can fix this issue with list mutation. In Python, some objects, such as lists and dictionaries, are mutable, meaning that their contents or state can be changed over the course of program execution. Therefore, instead of building a new pizza, we can just mutate pizza to add some onions!
1 | >>> pizza.append('onions') |
append
is what’s known as a method, or a function that belongs to an object, so we have to call it using dot notation. Don’t worry too much about the details of methods; we will talk more about them later on in the course. For now, here’s a list of useful list mutation methods:
append(el)
: Addsel
to the end of the listextend(lst)
: Extends the list by concatenating it withlst
insert(i, el)
: Insertel
at indexi
(does not replace element but adds a new one)remove(el)
: Removes the first occurrence ofel
in list, otherwise errorspop(i)
: Removes and returns the element at indexi
We can also use the familiar indexing operator with an assignment statement to change an existing element in a list. For example, let’s say you want to replace mushrooms on your pizza with tomatoes. We can index into the list at index 1 and reassign it to ’tomatoes’ like so:Although lists and dictionaries are mutable, many other objects, such as numeric types, tuples, and strings, are immutable, meaning they cannot be changed once they are created.1
2
3>>> pizza[1] = 'tomatoes'
>>> pizza
['cheese', 'tomatoes', 'onions']
Question
1.1 What would Python display? In addition to giving the output, draw the box and pointer diagrams for each list to the right.
1 | >>> lst1 = [1, 2, 3] |
1.2 Write a function that takes in a value x
, a value el
, and a list and adds as many el
’s to the end of the list as there are x
’s. Make sure to modify the original list using list mutation techniques.
1 | def add_this_many(x, el, lst): |
2 Nonlocal
Until now, you’ve been able to access names in parent frames, but you have not been able to modify them. The nonlocal keyword can be used to modify a binding in a parent frame. For example, consider stepper, which uses nonlocal to modify num:
1 | def stepper(num): |
As illustrated in this example, nonlocal
is useful for maintaining state across different calls to the same function.
However, there are two important caveats with nonlocal names:
- Global names cannot be modified using the nonlocal keyword.
- Names in the current frame cannot be overridden using the nonlocal keyword.
This means we cannot have both a local and nonlocal binding with the same name in a single frame.
Becausenonlocal
lets you modify bindings in parent frames, we call functions that use it mutable functions.
Question
2.1 Draw the environment diagram for the following code.
1 | def stepper(num): |
2.2 The bathtub below simulates an epic battle between Finn and Kylo Ren over a populace of rubber duckies. Fill in the body of ducky so that all doctests pass.
1 | def bathtub(n): |
3 Iterators and Generators
An iterable is a data type which contains a collection of values which can be processed one by one sequentially. Some examples of iterables we’ve seen include lists, tuples, strings, and dictionaries. In general, any object that can be iterated over in a for loop can be considered an iterable.
While an iterable contains values that can be iterated over, we need another type of object called an iterator to actually retrieve values contained in an iterable. Calling the iter function on an iterable will create an iterator over that iterable. Each iterator keeps track of its position within the iterable. Calling the next function on an iterator will give the current value in the iterable and move the iterator’s position to the next value.
1 | >>> a = [1, 2] |
In this way, the relationship between an iterable and an iterator is analogous to the relationship between a book and a bookmark - an iterable contains the data that is being iterated over, and an iterator keeps track of your position within that data.
Once an iterator has returned all the values in an iterable, subsequent calls to next on that iterable will result in a StopIteration exception. In order to be able to access the values in the iterable a second time, you would have to create a second iterator.
One important application of iterables and iterators is the for loop. We’ve seen how we can use for loops to iterate over iterables like lists and dictionaries.
This only works because the for loop implicitly creates an iterator using the builtin iter function. Python then calls next repeatedly on the iterator, until it raises StopIteration
.
The code shows how we can mimic the behavior of for loops using while loops.
1 | counts = [1, 2, 3] |
Note that most iterators are also iterables - that is, calling iter on them will return an iterator. This means that we can use them inside for loops. However, calling iter on most iterators will not create a new iterator - instead, it will simply return
the same iterator.
We can also iterate over iterables in a list comprehension or pass in an iterable to the built-in function list in order to put the items of an iterable into a list.
In addition to the sequences we’ve learned, Python has some built-in ways to create iterables and iterators. Here are a few useful ones:
- range(start, end) returns an iterable containing numbers from start to end1. If
start
is not provided, it defaults to 0. - map(f, iterable) returns a new iterator containing the values resulting from applying f to each value in
iterable
. - filter(f, iterable) returns a new iterator containing only the values in iterable for which f returns
True
Question
3.1 What would Python display? If a StopIteration Exception occurs, write StopIteration, and if another error occurs, write Error.
1 | >>> lst = [6, 1, "a"] |
Generators
A generator function is a special kind of Python function that uses a yield statement instead of a return statement to report values. When a generator function is called, it returns an iterator. To the right, you can see a function that returns an iterator over the natural numbers.
1 | def gen_naturals(): |
The yield statement is similar to a return statement. However, while a return statement closes the current frame
after the function exits, a yield statement causes the frame to be saved until the next time next is called, which allows the generator to automatically keep track of the iteration state.
Once next is called again, execution resumes where it last stopped and continues until the next yield statement or the end of the function. A generator function can have multiple yield statements.
Including a yield statement in a function automatically tells Python that this function will create a generator. When we call the function, it returns a generator object instead of executing the body. When the generator’s next method is called, the body is executed until the next yield statement is executed.
When yield from is called on an iterator, it will yield every value from that iterator. It’s similar to doing the following:
1 | for x in an_iterator: |
The example to the right demonstrates different ways of computing the same result.
1 | lambda x: x*x square = |
Questions
3.2 What would Python display? If a StopIteration Exception occurs, write StopIteration
, or if another error occurs, write Error
.
1 | def weird_gen(x): |
3.3 Write a generator function gen_all_items
that takes a list of iterators and yields items from all of them in order.
1 | def gen_all_items(lst): |
The yield from solution is nice and short. But this can also be done with just yield:
1 | for it in lst: |
Notice that this function will not yield out of deep lists. That is, it will keep the brackets around deep lists and yield them together instead of one element at a time.
For an extra challenge, figure out how to yield deep list items! (so the example in the doctest would return [1, 2, 3, 4, 5, 6])