delete a Python object that matches an atribute - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: General Coding Help (https://python-forum.io/forum-8.html) +--- Thread: delete a Python object that matches an atribute (/thread-24522.html) |
delete a Python object that matches an atribute - portafreak - Feb-18-2020 Hi, How do I delete a Python object that matches an atribute? I have the following code and I would like to delete created object with the attribute IP = 10.192.168.1. It is not working, it only works if I do it directly like this: del id1. I want to be able to delete the object if it matches an IP address. import gc class tech_dispo: tech_list = [] techCount = 0 def __init__(self, nom, ip,pc): if (nom in tech_dispo.tech_list): print ("Tech in list") else: self.nom = nom self.ip = ip self.pc = pc tech_dispo.techCount += 1 tech_dispo.tech_list.append(nom) def display_total_tech(self): print ("Total tech %d" % tech_dispo.techCount) return tech_dispo.techCount def displaytech(self): print (self.nom,self.ip,self.pc) id1 = tech_dispo("carl", "10.192.168.1","PC-01") id2 = tech_dispo("Stéphane", "10.192.168.2","PC-02") # Check created objects in class for obj in gc.get_objects(): if isinstance(obj, tech_dispo): print(obj) # Delete object with IP = 10.192.168.1 for obj in gc.get_objects(): if isinstance(obj, tech_dispo): if (obj.ip == "10.192.168.1"): print("delete") del obj for obj in gc.get_objects(): if isinstance(obj, tech_dispo): print(obj) RE: delete a Python object that matches an atribute - Gribouillis - Feb-19-2020 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.
RE: delete a Python object that matches an atribute - portafreak - Feb-19-2020 Thanks a lot, it is working great. I really apreciate it. |