I want Python to return the same object when the instances are created with the similar attributes. Also when I am changing some attribute of one instance, that attribute should be synchronized between other instances also.
class Session:
def __init__(self, id):
self.id = id
self.data = None
session1 = Session(1)
session1.data = 202
print(session1.data) # 202
print(id(session1)) # should be 1111
session11 = Session(1)
print(session11.data) # 202
print(id(session11)) # should be the same as session1 -> 1111
session2 = Session(2)
print(id(session2)) # 2222
You can see above that I want session1
and session1a
be the same object because of Session(1)
, while session2
is another object because of Session(2)
.
How could I do it?
Aucun commentaire:
Enregistrer un commentaire