[Python] How can I delete an instance of a class within that class?

Discussão em 'Python' iniciado por Stack, Março 28, 2025.

  1. Stack

    Stack Membro Participativo

    I want to know how you can delete a instance within the class. I am trying del self, but it doesn't seem to work. Here's my code:

    class Thing:

    def __init__(self):
    self.alive = True
    self.age = 0

    def update(self):
    self.age += 1

    def kill(self):
    if self.age >= 10:
    del self

    def all(self):
    self.update()
    self.kill()

    things = []

    for i in range(0, 10):
    thing.append(Thing())

    while True:
    for thing in things:
    thing.all()


    I specifically want to delete the instance inside the class. I have also replaced del self with self = None, but this statement doesn't seem to have any effect. How can I do this?

    Continue reading...

Compartilhe esta Página