Feb-19-2020, 08:38 AM
The problem is that you are trying to use the garbage collector as a container for the tech_dispo instances. That's not the way it should be used. In normal code, one does not interact directly with the garbage collector. When you need a container for a collection of instances, just create one. See the below example: I create a class TechDispoTeam, which is just a kind of dictionary to keep the instances. Potentially, I could have several teams
class TechDispo: def __init__(self, nom, ip, pc): self.nom = nom self.ip = ip self.pc = pc def __repr__(self): return "TechDispo{}".format((self.nom, self.ip, self.pc)) class TechDispoTeam(dict): def tech_dispo(self, nom, ip, pc): if nom not in self: self[nom] = TechDispo(nom, ip, pc) return self[nom] team = TechDispoTeam() t1 = team.tech_dispo('carl', '10.192.168.1', 'PC-01') t2 = team.tech_dispo('Stéphane', '10.192.168.2', 'PC-02') print('There are {} techs in the team'.format(len(team))) # show all instances for t in team.values(): print(t) # delete instance with ip '10.192.168.1' for nom, t in list(team.items()): if t.ip == '10.192.168.1': del team[nom] print('There are {} techs in the team'.format(len(team)))Several questions could be addressed: what to do if the team recruits a second Carl? What to do if we call
team.tech_dispo('carl', '10.192.168.7', 'PC-5')
but Carl is already in the team? Should the new IP and PC replace the old ones for Carl? The current code ignores the new settings, etc.