0

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!

  • Maybe this answer will help? https://stackoverflow.com/questions/12101958/how-to-keep-track-of-class-instances – Dan Aug 16 '19 at 15:41
  • Thank you for this answer too. Yes it helped, but I didn't see your comment at first and the one @SteveJ sent was ok for me too. Thank you anyway!!! – Anafulchtrudel Aug 17 '19 at 21:24

1 Answers1

0

I'm not 100% sure what you are trying to do with sub, but let me know if this gets you closer. Add a comment if you need follow-up and I'll help any way I can;

from typing import Dict


class Mother():

    all_mothers = dict()                            # type: Dict[str, Mother]

    def __init__(self, last_name, first_name):
        self.last_name = last_name                  # type: str
        self.first_name = first_name                # type: str
        Mother.all_mothers[last_name] = self


jones = Mother("Jones", "Martha")
smith = Mother("Smith", "Sarah")

print(smith.first_name)
print(Mother.all_mothers['Smith'].first_name)

smith.first_name = "Jane"
print(smith.first_name)
print(Mother.all_mothers['Smith'].first_name)

Mother.all_mothers["Jones"].first_name = "Sue"
print(jones.first_name)

Sarah
Sarah
Jane
Jane
Sue

SteveJ
  • 3,034
  • 2
  • 27
  • 47
  • Thank you for the answer!!! It fits perfectly for what I need. In fact I did it already but seeing your code made me realize that I only obained a repr of my instance, but it fits perfectly well now! Also, I made a test to alter directly an instance's value, and it reported dynamicly in the dict, so it's really perfect! Thank you very much! – Anafulchtrudel Aug 16 '19 at 21:23
  • also as an additive, and after some tests, it seems like all instances and duplicates autoupdates even if presents in another instance, which is perfect for the use I want to do. – Anafulchtrudel Aug 16 '19 at 21:51
  • That is great, I'm glad it worked for you! If you are satisfied with the answer, please mark this as the answer so that others can benefit from your question. Happy coding! – SteveJ Aug 16 '19 at 22:00