Python Forum
delete a Python object that matches an atribute
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
delete a Python object that matches an atribute
#2
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.
Reply


Messages In This Thread
RE: delete a Python object that matches an atribute - by Gribouillis - Feb-19-2020, 08:38 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  python update binary object (override delivered Object properties) pierre38 4 1,936 May-19-2022, 07:52 AM
Last Post: pierre38
  DELETE Post using Python FaceBook Graph API BIG_PESH 0 1,570 Mar-24-2022, 08:28 PM
Last Post: BIG_PESH
  Why doesn't gc delete an object without forcing a garbage collection call? AlekseyPython 5 3,930 Mar-19-2019, 02:10 AM
Last Post: micseydel
  Not Able To Delete First Node From Python Linked List ribena1980 8 4,506 Mar-05-2019, 03:14 PM
Last Post: ichabod801
  Detecting if image matches another kainev 2 2,991 Dec-02-2018, 02:00 PM
Last Post: kainev
  How to detect and tell user that no matches were found in a list RedSkeleton007 6 4,102 Jul-19-2018, 06:27 PM
Last Post: woooee
  for any loop _ want to know which matches 3Pinter 8 4,626 Jun-14-2018, 10:04 AM
Last Post: buran
  Find duplicate images and delete them using python and openCVq Prince_Bhatia 2 10,134 Dec-05-2017, 05:52 PM
Last Post: Prince_Bhatia
  Unable to delete duplicates in excel with Python tysondogerz 2 9,822 Nov-07-2017, 11:25 AM
Last Post: tysondogerz
  checking for matches between lists henrik0706 2 2,803 Oct-24-2017, 11:08 AM
Last Post: henrik0706

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020