0

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.

4
  • Is there some other container somewhere that holds the task instances? Where are you trying to delete the instance from? Commented Oct 17, 2019 at 11:18
  • no, i tried a simple example. saved an instance and then tried to delete him immediately. Commented Oct 17, 2019 at 11:56
  • How can you tell the object wasn't deleted? Commented Oct 17, 2019 at 11:59
  • id try to call him again in the interpeter Commented Oct 17, 2019 at 12:02

1 Answer 1

1

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?

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

5 Comments

so there is another way to do it and to make sure that the instance is not exist anymore?
The instance will automatically be removed once there are no references to it. In general unless you have a very specific reason for it, you should let garbage collection handle such things for you.
but my idea is immediately remove the instance. i gonna use a func that gonna show all the task that connected to the current day, and i want that instance that i marked as complete won't appear
In that case it would be more straightforward to remove the task from whatever data structure you're using to store the tasks, or to mark it as complete or something, so that when you display tasks you don't display it.
If you do a = reg_task() and then the instance deleted itself, what would you expect to happen to the following code that tries to use a?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.