2

I wanted to make sense of the following code variations:

a = [1, 2, 3]
b = a
b.append(4)
b = ['a', 'b']
print(a, b)

What I understood was that variable a refers to an object that contains the list [1,2,3] in some place in memory, and b now is referring to the same object that a is referring to, and via that link we're technically appending in a not b.

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

I updated the code a bit:

a = [1, 2, 3]
b = ['a', 'b']
b = a
b.append(4)
print(a, b)

My understanding: b is now referring to two objects, the first list ['a','b'] and the second list (that a is initially referring to) [1,2,3] via the third line b = a.

Output: [1, 2, 3, 4] [1, 2, 3, 4]

Last Code variation:

a = [1, 2, 3]
b = ['a', 'b']
b = a
b.append(4)
a.append(10)
print(a, b)

based on my understanding so far, I though that the link on line 3 b = a was giving only b the ability to reference multiple objects (it's own and a's) and a should've only be referencing one object [1,2,3], so the expected output should be: [1,2,3,4,10] [1,2,3,4]

Actual Output: [1, 2, 3, 4, 10] [1, 2, 3, 4, 10]

So is this assignment on line 3 b = a is like a bi-directional link? where also a reference is created by a to b's object?

7
  • 1
    the variable a and the variable b refer to the same list . if you change one of them, the other also recieves these changes. If you want to copy a list, use new_list = list.copy() This may also help: programiz.com/python-programming/methods/list/copy Commented Aug 14, 2020 at 17:35
  • 1
    Why do you make this so complicated? After b = a, a and b are the same object, that's all there is to it. Commented Aug 14, 2020 at 17:35
  • 1
    "we're technically appending in a not b." no. Variables refer to objects. Both variables are referring to the same object and you append to that object. You don't append to a variable. "My understanding: b is now referring to two objects" Well, not at the same time. It referred to one object at the beginning, and refers to another later. Read the following: nedbatchelder.com/text/names.html Commented Aug 14, 2020 at 18:18
  • 2
    Note, this has nothing to do with "passing by assignment", which refers to an evaluation strategy, which has to do with how and when function arguments are evaluated, not simple assignment like this. Commented Aug 14, 2020 at 18:20
  • 2
    "based on my understanding so far, I though that the link on line 3 b = a was giving only b the ability to reference multiple objects (it's own and a's) and a " no. variables can only ever refer to one object at a time. I think this is what is confusing you. Commented Aug 14, 2020 at 18:21

3 Answers 3

9

My understanding: b is now referring to two objects, . . .

That is not correct. A name cannot be associated with multiple objects within a given scope at the same time.

b = a associates b with the object that a is associated with. After that line has executed, nothing will be referencing ['a', 'b'], and that list should be eligible for garbage collection because it can no longer be used.

Sign up to request clarification or add additional context in comments.

Comments

8

Don't think of the objects like pointers, I think that is the source of your confusion. It is not that "b points to a" or "a points to b", it has to do with binding to an object. I think looking at id will be useful

>>> a = [1, 2, 3]
>>> id(a)
1833964774216
>>> b = a
>>> id(b)
1833964774216

In this case both a and b are bound to that list. So any mutation to list 1833964774216 will be reflected in both objects. But I can re-assign (or re-bind) to a completely different object

>>> b = [4, 5, 6]
>>> id(b)
1833965089992
>>> b
[4, 5, 6]

This has no effect whatsoever on a because it is still bound to the original list

>>> a
[1, 2, 3]
>>> id(a)
1833964774216

Comments

1

let’s understand the difference between names and objects. In your case, a and b are names, and the list "[1,2,3]" assigned to a and b is the object. Initially a = [1,2,3] and after a is assigned to b i.e. b=a, in this case both a and b are bound to the same list object. This means that you can change that list object’s value by using either of the names a or b.

This can be overcome by copying the list assigned to a to b as,

b = a.copy()

which will create a copy of same object assigned to b

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.