avifen
avifen

Reputation: 974

Function that does something and calls a built-in function that deletes it

I'm building a task manager and I want to use a complete-func that's going to do something with the instantiated and then call del and delete the instantiated class object. Is it possible? I'm trying hard to find a solution.

Tried to use a function from the class, and tried to find articles about this subject, but no success.

from datetime import date

class reg_task:
    def __init__(self, what_to_do, date=date.today(), hour=None, tag=None, project="day to day task", priority=None, remind_time=None):
        self.what_to_do = what_to_do
        self.date = date
        self.hour = hour
        self.tag = tag
        self.project = project
        self.priority = priority
        self.remind_time = remind_time

    def __str__(self):
        return f'task {self.what_to_do}, to-do-date - {self.date}'

    def tasks_complete(self):
        with open(r"C:\Users\Avi Fenesh\Desktop\python\tasks_project\archive\archive", "a") as archive:
            archive.write(f"{str(self)} \n")
        del self

The problem is with tasks_complete(). When I call the function it doesn't delete the instantiated class object.

Upvotes: 0

Views: 57

Answers (1)

hoodakaushal
hoodakaushal

Reputation: 1293

That's because objects can't be garbage collected as long as someone holds a reference to them. Simply doing del self is not enough.

See: del self vs self.__del__() - and what is the proper way to cleanup in python?

Upvotes: 1

Related Questions