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

Clique ou arraste solte itens e preencha os espaços

Tudo estava claro?

Como podemos melhorá-lo?

Obrigado pelo seu feedback!

Seção 4. Capítulo 2

Pergunte à IA

expand

Pergunte à IA

ChatGPT

Pergunte o que quiser ou experimente uma das perguntas sugeridas para iniciar nosso bate-papo

Awesome!

Completion rate improved to 5.26

bookObject References and Side Effects

Deslize para mostrar o menu

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]

Clique ou arraste solte itens e preencha os espaços

Tudo estava claro?

Como podemos melhorá-lo?

Obrigado pelo seu feedback!

Seção 4. Capítulo 2
some-alt