I suppose you know the idea of mutable and immutable objects. Immutable objects (str, int, float, etc) are passed by value. That's why your desired behaviour can't work. When you do:
a = 1
d["a"] = a
you just pass the value of a
variable to your dict key 'a'. d['a']
knows nothing about variable a
. They just both point to same primitive value in memory.
I don't know your case and why you need this behaviour, but you can try to use mutable object.
For example:
class A:
def __init__(self, a: int):
self._value = a
@property
def value(self) -> int:
return self._value
@value.setter
def value(self, a: int):
# you can put some additional logic for setting new value
self._value = a
def __int__(self) -> int:
return self._value
And then you can use it in this way:
>>> a = A(1)
>>> d = {"a": a}
>>> d['a'].value
1
>>> d["a"].value = 2
>>> d['a'].value
2
>>> a.value
2
>>> int(a)
2
But it still seems like an overhead and you should rethink whether you really need this behaviour.