Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Lära Object References and Side Effects | Mutable Objects and Function Arguments
Common Python Mistakes and How to Fix Them

bookObject References and Side Effects

12345
a = [1, 2, 3] b = a b.append(4) print("a:", a) print("b:", b)
copy

When you assign a variable to a mutable object like a list, you are actually creating a reference to that object, not a new copy. In the code above, both a and b point to the same list in memory. This means that any change made to the list through b—such as appending the value 4—will also be reflected when accessing a. This behavior can lead to unexpected side effects, especially when you intend to have two separate lists.

To avoid this, you can create a copy of the list. There are several ways to do this in Python, such as using the list() constructor, slicing, or the copy() method. For example, if you want b to be a new list with the same contents as a, you can write b = a.copy(). Now, modifying b will not affect a, and vice versa. Understanding how references work is crucial for avoiding bugs related to shared mutable objects.

1. Which statement best describes what happens when two variables reference the same list in Python?

2. Arrange the code blocks in the correct order to copy a list so that modifying the new list does not affect the original.

question mark

Which statement best describes what happens when two variables reference the same list in Python?

Select the correct answer

question-icon

Arrange the code blocks in the correct order to copy a list so that modifying the new list does not affect the original.

a: [1, 2, 3]
b: [1, 2, 3, 4]

Click or drag`n`drop items and fill in the blanks

Var allt tydligt?

Hur kan vi förbättra det?

Tack för dina kommentarer!

Avsnitt 4. Kapitel 2

Fråga AI

expand

Fråga AI

ChatGPT

Fråga vad du vill eller prova någon av de föreslagna frågorna för att starta vårt samtal

Suggested prompts:

Can you show examples of how to properly copy a list?

What are other common pitfalls with mutable objects in Python?

Can you explain the difference between shallow and deep copies?

Awesome!

Completion rate improved to 5.26

bookObject References and Side Effects

Svep för att visa menyn

12345
a = [1, 2, 3] b = a b.append(4) print("a:", a) print("b:", b)
copy

When you assign a variable to a mutable object like a list, you are actually creating a reference to that object, not a new copy. In the code above, both a and b point to the same list in memory. This means that any change made to the list through b—such as appending the value 4—will also be reflected when accessing a. This behavior can lead to unexpected side effects, especially when you intend to have two separate lists.

To avoid this, you can create a copy of the list. There are several ways to do this in Python, such as using the list() constructor, slicing, or the copy() method. For example, if you want b to be a new list with the same contents as a, you can write b = a.copy(). Now, modifying b will not affect a, and vice versa. Understanding how references work is crucial for avoiding bugs related to shared mutable objects.

1. Which statement best describes what happens when two variables reference the same list in Python?

2. Arrange the code blocks in the correct order to copy a list so that modifying the new list does not affect the original.

question mark

Which statement best describes what happens when two variables reference the same list in Python?

Select the correct answer

question-icon

Arrange the code blocks in the correct order to copy a list so that modifying the new list does not affect the original.

a: [1, 2, 3]
b: [1, 2, 3, 4]

Click or drag`n`drop items and fill in the blanks

Var allt tydligt?

Hur kan vi förbättra det?

Tack för dina kommentarer!

Avsnitt 4. Kapitel 2
some-alt