I'm looking for a way to delete instances of a class, the methods I see to safely do this are like the one proposed by Clint Miller here. For what I'm trying to do, this does not seem like a viable option.
My question is, is the method below safe for getting rid of class instances, and if not, how (if at all) can it be modified to make it so?
class myClass:
_reg = []
def __init__(self, *args):
self.args = args
self._reg.append(self)
...
def close(self):
self._reg.pop(self._reg.index(self))
Instances of myClass are only ever created like this: myClass(arg1, arg2...)
as opposed to this: a = myClass(arg1, arg2...)
or through a function like myFunc
. When the instance is no longer needed, its close function is called.
def myFunc(*args):
...
myClass(newArg1, newArg2...)
P.S You can assume that no new references will be made to items in myClass._reg
and thanks in advance.
Edit: The registry exists as a way of keeping track of instances of myClass
while they exist. It's not just an overcomplicated way of trying to immediately delete them (I probably wouldn't have created them were that the case).
The amount of time each instance needs to exist and perform functions that have been omitted for brevity (and relevance) varies a lot.
As for why I need to make sure unused instances are deleted new instances need to be created at unspecified times, and some of the intended functions of each instance will begin to clash with one another in ways that will be hard to deal with if they're not deleted or otherwise disabled. Besides that, the list of instances will almost certainly become extremely long as this happens which could cause other problems.