Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Lernen 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

War alles klar?

Wie können wir es verbessern?

Danke für Ihr Feedback!

Abschnitt 4. Kapitel 2

Fragen Sie AI

expand

Fragen Sie AI

ChatGPT

Fragen Sie alles oder probieren Sie eine der vorgeschlagenen Fragen, um unser Gespräch zu beginnen

Awesome!

Completion rate improved to 5.26

bookObject References and Side Effects

Swipe um das Menü anzuzeigen

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

War alles klar?

Wie können wir es verbessern?

Danke für Ihr Feedback!

Abschnitt 4. Kapitel 2
some-alt