-2

Below is the code

a =2 
b = a
b = 3
print (b,a)
3 2

I expect the value of a also to change to 3 , if b is only pointing to a's memory space and does not have it's own memory.

I am sure there is a very simple explanation which i am missing.

martineau
  • 119,623
  • 25
  • 170
  • 301
  • 3
    Assignment *never* creates a copy – Chris_Rands Sep 21 '18 at 09:32
  • 2
    Read this https://nedbatchelder.com/text/names.html – Chris_Rands Sep 21 '18 at 09:34
  • 4
    `b = 3` is not *modifying* an existing value; it binds a *new* value to `b`. – deceze Sep 21 '18 at 09:34
  • 1
    You can do `b = a` to point `b` at the same number as `a`, but then when you do `b = 3`, you point it somewhere else. – khelwood Sep 21 '18 at 09:34
  • 1
    There are good explanations in this question: [Why can a function modify some arguments as perceived by the caller, but not others?](//stackoverflow.com/q/575196) – Aran-Fey Sep 21 '18 at 09:36
  • Sounds like you need to get over thinking that Python variables are just like C/C++'s. Also, Python doesn't have pointers, so saying "`b` points to `a`" doesn't really make sense. – martineau Sep 21 '18 at 09:41

4 Answers4

2

No. Assignment copies the reference to the object, not the object itself. eg

a = [1, 2, 3]
b = a
b[0] = 5
print(a) # gives [5, 2, 3]

However,

b = a
b = 3

re-assigns b to a new integer literal, having nothing to do with a.

Also, not strictly part of your question, but see the difference between mutable and immutable objects.

blue_note
  • 27,712
  • 9
  • 72
  • 90
0

b = a points b to the same object as a. But then when you do b = 3 that reassigns b to point to a different object. a is not affected.

Daniel Roseman
  • 588,541
  • 66
  • 880
  • 895
0

The problem here is that you think of python variables a pointers, where is not bad at all but they don't work like that. You should think of python variables as labels to values. Also you have to think about mutable and inmutable data. Python strings and integers are inmutable, that means that python creates a new number for each operation and reassigns the variable to the new number.

As in your example:

--------------------------------
|     3    |    2   |   ..     |
--------------------------------

a = 2 # a points to place 1 in our example memmory block
b = a # b points to the same memmory block as a
b = 3 # b change to point to the memmory block where 3 is located, place 0

In case that:

a = 2 # a points to place 1 in our example memmory block
b = a # b points to the same memmory block as a
b += 1 # b == 2, b + 1 == 3, so b will point to a 3, a is still not modified
Netwave
  • 40,134
  • 6
  • 50
  • 93
0

if you reassign a value of b, python reassigns b which has a different value and memory loc. then. printing the address with id(a) and id(b) you can see that if values are different so the memory addresses are actually different.

try in python shell :

a = 3
b = a
id(b) == id(a) #it returns True

b = 2
id(b) == id(a) #it returns False