I understand why below works as such:
v = 1
d = dict()
d['hi'] = v
d['hi'] = 2
print(v) # prints 1 as expected
And also why this works:
class Node:
def __init__(self,val):
self.val = val
n = Node(1)
d['hi_node'] = n
d['hi_node'].val = 100
print(n.val) # prints 100 as expected
I also believe that I understand why the below works as such:
n2 = Node(2)
d['hi_n2'] = n2
n3 = Node(3)
val_in_d = d['hi_n2'] # creates a new binding of name val_in_d
val_in_d = Node(3) # updates the location to which val_in_d points
print(n2.val) # still 2
But is there any way to point n2
to n3
using dictionary d
which has the similar impact as writing n2 = n3
? So, finally if I print n2.val
, I get 3
i.e. I wish to take the value stored in the dictionary and change it's reference to point to another object rather than just updating the value that is stored in dictionary.
Any help would be highly appreciated.