0

Look at the code:

a = ['Hello']
b = a          # ['Hello']

b.append(2)
print b        # ['Hello', 2]
print a        # ['Hello', 2]

Here, a is assigned to b, meaning that the change of value in a can affect the value of b. How can the change in b can affect a in this case?

Is it that List in python have any special rule where appending a value can affect both a and b?

Jay
  • 41
  • 1
  • 10
  • Nothing special about reference copies. – cs95 Oct 25 '17 at 06:43
  • Please go through https://stackoverflow.com/questions/8056130/immutable-vs-mutable-types – itzMEonTV Oct 25 '17 at 06:43
  • No, **all python objects work this way**. please read Ned Batchelder's [Facts and Myths about python names and values](https://nedbatchelder.com/text/names.html) – juanpa.arrivillaga Oct 25 '17 at 06:43
  • The link you sent has good explanations, but the thing I asked was reverse than the 5th Fact mentioned i.e. (Fact: Assignment never copies data.) @juanpa.arrivillaga – Jay Oct 25 '17 at 06:58

1 Answers1

0

Because it's exactly the same value. Names are bound to references, which means that b = a points both to the same list.

Ignacio Vazquez-Abrams
  • 776,304
  • 153
  • 1,341
  • 1,358