My problem is that I'd like to save instances of a class in a class dict (here named catalog). Each time I create a new instance, I want it to be stored in catalog, the keys being the self.id value, and the value being the instance itself.
I already looked for some solution with new, but it seems like new can only return an instance and dont initialize it, as init do the job.
def Mother():
id_m=0
catalog={}
def __init__(self):
self.value=0
self.id=None
self.sub_dict={}
self.id_attrib()
Mother.id_m+=1
def id_attrib(self):
if self.id==None:
self.id=id_m
else:
pass
def __sub__(self,sub):
if type(sub) is not Mother:
return self
else:
index=0
while index not in self.sub_dict.keys():
index+=1
self.sub_dict[index]=sub
So far, this code only initialize a new instance. What I want to do further is to provide a class method that updates instances in self.sub_dict.
s1=Mother()
s2=Mother()
s1=s1-s2 ## adds s2 to the self.sub_dict
s2.value=150 ##How to update the value in self.sub_dict?
Thanks for your answers!