簡體   English   中英

Function 執行某些操作並調用刪除它的內置 function

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

我正在構建一個任務管理器,我想使用一個完整的函數來對實例化做一些事情,然后調用del並刪除實例化的 class object。 可能嗎? 我正在努力尋找解決方案。

嘗試使用 class 中的 function,並嘗試查找有關此主題的文章,但沒有成功。

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

問題在於tasks_complete() 當我調用 function 時,它不會刪除實例化的 class object。

這是因為只要有人持有對對象的引用,就不能對對象進行垃圾回收。 僅僅做del self是不夠的。

請參閱: del self vs self.__del__() - 在 python 中清理的正確方法是什么?

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM