1

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.

Community
  • 1
  • 1
Okinawa Sama
  • 55
  • 1
  • 1
  • 5
  • 1
    Why are you saving references to the instances? Why do you need to explicitly delete the instances? – jonrsharpe Apr 17 '16 at 20:50
  • 1
    You could, but what's the point? If you're never assigning the instances to a variable, they'll simply be destroyed when they go out of scope anyway. So why add a registry that you don't need, only to do something that would happen anyway? – Daniel Roseman Apr 17 '16 at 20:51
  • The registry has been added because it's an easy way to have another function (not mentioned here) be able to check the status of all instances of myClass and I need to hold them for an unspecified amount of time. – Okinawa Sama Apr 17 '16 at 21:00
  • 1
    Explicit is better than implicit (the Zen of Python). Do `a = myClass(arg1, arg2, ...)` and then `my_common_list.append(a)`. Or create a `function` (IMHO should be separated from the class) that will do that for you in one go. But don't put that in `__init__`. Generally you don't want this kind of side effects in `__init__`. Plus the `_reg` list should be separated from `myClass` as well IMHO. The code you've shown us will be hard to maintain. BTW `close` method is not thread safe. – freakish Apr 17 '16 at 21:02
  • I had planned to have some external function create instances as necessary, so that part's fine. What type of side effects do you think might arise from appending in `__init__` or having `_reg` be part of `myClass`? I ask because I genuinely don't know and could be doing things that are similarly problematic elsewhere. – Okinawa Sama Apr 17 '16 at 21:21
  • 1
    In theory, what you currently have should suffice. As soon as an object is popped from `_reg` its reference count should drop to 1 (the reference count is generally 1 higher than you expect) and the garbage collector will clean it up. You can also force the cleanup there and then in `close()` by calling `gc.collect()`. You'd have to play around with the `gc` module a bit to make sure what you're expecting is happening. This all sounds pretty nasty though. – s16h Apr 17 '16 at 22:24

0 Answers0