I have the following code:
a=(0,1)
x={a}
I changed a
as
a=(1,2)
but x
remains the same as before. I am wondering how I can change x
whenever I change a
?
I have the following code:
a=(0,1)
x={a}
I changed a
as
a=(1,2)
but x
remains the same as before. I am wondering how I can change x
whenever I change a
?
You can create an object to hold your data. The object implements __hash__
, so it can be used in a set.
class Container:
def __init__(self, data=None):
self.data = data or []
def __repr__(self):
return str(self.data)
x = {a}
print(x) # {[]}
a.data = [0, 1, 2]
print(x) # {[0, 1, 2]}
See https://stackoverflow.com/a/31340810/5666087 for more information.
Switch over to a list and you could do. Otherwise it's not possible with your variables.
a=[0,1]
x=[a]
print(a)
print(x)
a[0]=1
a[1]=2
print(a)
print(x)