We will discuss how lists are stored so that we can understand what
happens when they are copied. The ideas of aliasing and references
will be useful more generally.
# To prepare for lecture:
### To read/watch before lecture:
- This video does a good job of explaining how lists are stored and
what aliasing is, then giving an example of code that is incorrect
because of aliasing:
Python - Episode 7 - Aliasing
- The first 3:40 minutes of the following video gives another
explanation of how lists are stored and then gives some ways to get a
shallow copy (the rest of the video does talk about shallow/deep
copies, but does so using objects/classes, which we haven't talked
about yet (but will in 2 weeks)):
Shallow Copies
versus Deep Copies: What you need to know to work with lists
effectively and bug-free
- Finally, the first 1:20 minutes of this video says a little more
about
copying: How to
copy a list in Python. (Yes, the entire video is only a minute
and a half long; the last 10 seconds looks at how to do this in Python
2 as opposed to Python 3.)
- Read sections 10.10, 10.11, and 10.12 in How to Think Like a Computer Scientist: Interactive Edition
- Keep going on part 2 of assignment 8.
### Worksheet:
We'll be going over questions from the
worksheet exercise28.pdf.
- The first 3 questions look at how lists (and objects, more generally)
are stored. We will talk about aliasing and references in order
to understand why the exercises print what they do.
- Questions 4, 5, and 6 look more closely at how .copy() works and
highlights the distinction between a shallow copy and a deep copy.
- Question 7 is deliberately confusing and meant to be a
challenge question. Feel free to skip it . . . I may well end
up skipping it.
- Question 8 is an open-ended question that we will talk through as
time permits. It will motivate the next topic in the course.
# Notes from after lecture:
- We went through exercises 1-6 from the worksheet, discussing
references, aliasing, and the distinction between a shallow copy
and a deep copy. I drew a lot of diagrams, so thankfully I
correctly saved the marked up version of the worksheet and it's
available here. The code
is here.
- The video is on Box and available
here.
I've editted out the quiet time during the two polls (exercises
2 and 3) and the breakout room (for exercise 5).