Docsity
Docsity

Prepare for your exams
Prepare for your exams

Study with the several resources on Docsity


Earn points to download
Earn points to download

Earn points by helping other students or get them with a premium plan


Guidelines and tips
Guidelines and tips

Aliasing and Mutability in Python: Understanding Lists and Memory, Study notes of Signals and Systems Theory

The concept of aliasing and mutability in Python, focusing on lists. It covers how lists differ from immutable data types, the impact of aliasing on mutable variables, and the use of destructive and non-destructive functions. Additionally, it introduces 2D lists and their application in handling multi-dimensional data.

Typology: Study notes

2021/2022

Uploaded on 09/12/2022

ekapad
ekapad 🇮🇳

5

(17)

266 documents

1 / 33

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Aliasing and Mutability
15-110 Wednesday 02/19
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19
pf1a
pf1b
pf1c
pf1d
pf1e
pf1f
pf20
pf21

Partial preview of the text

Download Aliasing and Mutability in Python: Understanding Lists and Memory and more Study notes Signals and Systems Theory in PDF only on Docsity!

Aliasing and Mutability

15 - 110 – Wednesday 02/

Learning Goals

  • Recognize how aliasing impacts the values held in mutable variables
  • Recognize the difference between functions on mutable values that are destructive vs. non-destructive
  • Use 2D lists when reading and writing code to work on data over multiple dimensions

List Values Can Be Changed

Unlike the previous types we've worked with, the values in a list can be changed directly , without creating a new list that needs to be assigned to the variable. We can change a list by setting a list index to a new value, like how we would set a variable to a value. lst = [ "a", "b", "c" ] lst[1] = "foo" print(lst) # [ "a", "foo", "c" ]

Lists are Mutable; Strings are Immutable

We call data types that can be modified after being assigned mutable. Data types that cannot be modified directly are called immutable. All the other data types we've learned about so far – integers, floats, Booleans, and strings – are immutable. In fact, if we try to set a string index to a new character, we'll get an error. We have to set the entire variable equal to a new value if we want to change the string. s = "abc" s[1] = "z" # TypeError s = s[:1] + "z" + s[2:]

Lists in Memory

Data in Memory: Mutable vs Immutable

Recall that all the data we work with in a program must eventually be stored in the computer's memory as binary. Data is stored in memory differently based on whether it is mutable or immutable. Let's compare how memory works for strings (immutable) vs. lists (mutable).

Strings, Numbers, Booleans are Immutable

When we modify the value of immutable values , such as strings, numbers, and Booleans, Python makes a new value and reassigns the variable to reference the new value. s = "Hello" s = s + " World" t = s s Hello (^) Hello World Hello World t

Lists are Mutable

When we set a variable to a list , Python sets aside a large place in memory for the list. By breaking up that large chunk of memory into parts, Python can assign each value in the list a location, ordered sequentially. x = [1, 2, 3] x 1 2 3

Copying Lists in Memory

The use of references causes an interesting behavior when we copy a mutable variable x to a new variable y. The reference is copied, not the values. That means the same set of values is used for both variables. This is called aliasing. x = [1, 2, 3] y = x x 1 2 3 y

Copying References vs. Copying Values

Two variables won't be aliased just because they contain the same values. They need to refer to the same location in memory to be aliased. In the following example, the lack of a reference copy keeps the list z from being aliased to x and y. x = [1, 2, 3] y = x z = [1, 2, 3] x (^1 2 ) y z 1 2 3

Activity: Code Tracing

After the following code is executed, what is the value of x? x = [10, 20, 30] y = x x[2] = "dog" y.append("cat")

Check For Aliased Lists with id() and is

If you want to check whether two variables are aliased, you can use a built-in function and an operation. The function id(var) takes in a variable and returns the memory ID that Python associates with it. If two mutable variables have the same memory ID, they're aliased. The is operation returns True if two variables have the same ID, and False otherwise. a = [1, 2, 3] b = a c = [1, 2, 3] print(a is b) # True print(a is c) # False (^17)

Two Ways of Modifying Lists

Whenever we want to modify a list (by changing a value, adding a value, or removing a value), we can choose to do so destructively or non-destructively. Destructive approaches change the data values without changing the variable reference. Any aliases of the variable will see the change as well, since they refer to the same list. Non-destructive approaches make a new list, giving it a new reference. This 'breaks' the alias, and doesn't change the previously-aliased variables.

Two Ways to Update Values

How do we update a value in a list destructively? Use index assignment. lst = [1, 2, 3] lst[1] = "foo" How do we update a value in a list non-destructively? Use variable assignment with list slicing and concatenation. lst = [1, 2, 3] lst = lst[:1] + ["foo"] + lst[2:]