Because in Python, variables stores the references (not Values). The statement node5 = node1
actually makes node5
to point to an object. node1
is also pointing to the same object. If you make changes to that object using either node1
or node5
and print the results using either node1
or node5
, it will print the same result.
The different behavior that you have been observing using int
is because int
is immutable in Python. If you change the object's value, python creates a different object, stores the new value in it and updates the b
to point to that object (b = a
). After this statement, a
will be still pointing to the old object having value 2
. If you try your second code with any of the immutable in Python, you will get the same result.
If you want to copy node1 to node5, use deepcopy:
import copy
class Node:
def __init__(self, value = None, next = None):
self.value = value
self.next = next
def __str__(self):
return str(self.value)
node1 = Node(1)
node2 = Node(2)
node3 = Node(3)
node4 = node1
node5 = copy.deepcopy(node1)
node1.next = node2
node2.next = node3
def printList(node):
while node:
print(node, end=" ")
node = node.next
print("")
print("Node1 Id", id(node1), end=" ")
printList(node1)
print("Node4 Id", id(node4), end=" ")
printList(node4)
print("Node5 Id", id(node5), end=" ")
printList(node5)
Outputs:
Node1 Id 140465951784072 1 2 3
Node4 Id 140465951784072 1 2 3
Node5 Id 140465951810560 1
EDITED:
Added id() in my answer as suggested by @user3159253. You can see that id()
returned by node1
and node4
are same. That means, they indeed point to the same object and any change made using any one of the variable (node4 or node1), it will change the same single object and hence, you will get the same result if you print using either node1 or node4.
However, if you try your second code with id()
:
a = 2
b = a
print("Id of a", id(a), "Value of a", a)
print("Id of b", id(b), "Value of b", b)
a = 5
print("Id of a", id(a), "Value of a", a)
print("Id of b", id(b), "Value of b", b)
Output:
Id of a 10914528 Value of a 2
Id of b 10914528 Value of b 2
Id of a 10914624 Value of a 5
Id of b 10914528 Value of b 2
As you can see after the assignment b = a
, a
and b
were pointing to the same object but once you reassigned a
's value to 5, python created the different object (and hence, id() returned different value) and changed a
to point to that object having value 5. It did so because int
are immutable in Python.