List Aliasing / Mutation

Flash and JavaScript are required for this feature.

Download the video from iTunes U or the Internet Archive.

Description: This in-class exercise demonstrates list how aliasing affects other list operations in Python.

Instructor: Dr. Ana Bell

The following content is provided under a Creative Commons license. Your support will help MIT OpenCourseWare continue to offer high-quality educational resources for free. To make a donation or view additional materials from hundreds of MIT courses, visit MIT OpenCourseWare at ocw.mit.edu.

ANA BELL: All right, so let's see. So first we have L1 is equal to bacon, eggs. I'm going to short form-- shorthand this. L2 is equal to toast and jam. Brunch-- I should actually make these arrows-- brunch is equal to L1.

So this is just aliasing, which means brunch is going to point to whatever L1 object points to. And if I do L1.append juice, L1 is now going to be bacon, eggs, and juice. OK?

This L1 has been mutated to be that. And since brunch still points to the same object that L1 points to, brunch is now going to point to there. OK? So when I do brunch.extend L2, I'm going to take whatever brunch is, which is this part here, and I'm going to extend it by L2, which is toast and jam. OK?

So it's just going to contain a large list of those five elements because of this side effect issue, where brunch was pointing to the same thing that L1 was pointing to, OK? So it's close, but I think-- perfect. Yes, that's the right answer.

Free Downloads

Video


Caption

  • English-US (SRT)