2

A class is defined like this:

class IterRegistry(type):
    def __iter__(cls):
        return iter(cls._registry)
class Example:
    __metaclass__ = IterRegistry
    _registry  =[]
    def __init__(self,iD):
        self.iD = iD
        self.count = 40
        self._registry.append(self)
    def reduceCount(self):
        self.count -= 1

Over the course of the program more and more instances of the class are created. I have an timer running, which then runs a for Loop over all the instances and reduces the count by 1 for each.

def timer():
    for i in Example:
        i.reduceCount()
        if i.count < 0:
            #then delete the instance

My question is how do I delete this instance?

3
  • 3
    Python is a garbage collected language (using reference counting). Objects are deleted automatically if they become unreachable (their reference count becomes 0). Commented Oct 23, 2016 at 17:13
  • @UnholySheep How can I make the instance in this case become unreachable, to delete it? Commented Oct 23, 2016 at 19:03
  • By removing all references to it, either using del as suggested in the Answer below, by setting the variable that references it to None or if it's inside a container (list, set, dict, ...) by removing it from there. (This assumes that no other variables are referencing this object, you can't force the deletion of an object that is still being referenced) Commented Oct 23, 2016 at 19:08

2 Answers 2

2

You may use a del statement.

del can also be used to delete entire variables:

>>> del a

Referencing the name a hereafter is an error (at least until another value is assigned to it).

But at the end of the day, this is just a suggestion to the garbage collector. Object may be deleted right away, but it also may be deleted long after execution of this statement. It's not guaranteed by a language specification.

Sign up to request clarification or add additional context in comments.

2 Comments

@LukasRogalski I have tried this but this does not work, when I use the del Statement in the loop. As the loop works continues to run for the same instance. I believe this is because the variable "points" to the instance since it is in a for loop and it is not the instance itself. But this is mere speculation.
@Fusight if you del the iterating variable in a for loop you are only forcing that variable to no longer hold a reference to the object. The original reference (inside the list that contains the object) is unaffected by this
1

To answer my question I followed @Unholysheep advice and removed it from the registry in the class. To do this I had to change the code slightly by changing _reigstry to registry, so I can acess it from the rest of the program.

class IterRegistry(type):
    def __iter__(cls):
        return iter(cls.registry)
class Example:
    __metaclass__ = IterRegistry
    registry  =[]
    def __init__(self,iD):
        self.iD = iD
        self.count = 40
        self.registry.append(self)
    def reduceCount(self):
        self.count -= 1

This now allows me to remove the instance from inside the loop by:

def timer():
    for i in Example:
        i.reduceCount()
        if i.count < 0:
            #then delete the instance
            Example.registry.remove(i)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.