Feb-18-2020, 12:26 AM
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:
I want to be able to delete the object if it matches an IP address.
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)
