python - 在Python中删除对象实例

标签 python python-2.7

类的定义如下:

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

在程序的过程中,越来越多的类实例被创建。我有一个正在运行的计时器,然后它对所有实例运行 for 循环,并将每个实例的计数减 1。

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

我的问题是如何删除该实例?

最佳答案

您可以使用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).

但归根结底,这只是给垃圾收集器的一个建议。对象可能会立即被删除,但也可能在执行此语句后很久才被删除。它不受语言规范的保证。

关于python - 在Python中删除对象实例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40205571/

相关文章:

python - 特拉维斯建立。导入错误 : No module named gdal

python - 日期库,能够计算诸如 "every third tuesday"之类的东西?

python - 如何对一列数据执行行函数并将函数的输出附加到 Pandas data.frame 中?

python - 如何将实例成员的默认参数值传递给方法?

python - 使用 Python 从 txt 文件中仅提取所需的列?

python - 将记忆化应用于硬币兑换问题

python - 对行值进行排序并显示列顺序

python - 如何以编程方式确定函数/内置/可调用参数的数量?

python - Python 中字典的最大大小?

python - 根据给定数字按 -5 和 + 5 创建列表