Python:如何“杀死”一个类实例/对象?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16686788/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-18 23:22:19  来源:igfitidea点击:

Python: how to "kill" a class instance/object?

pythonclassoop

提问by user2407969

I want a Roach class to "die" when it reaches a certain amount of "hunger", but I don't know how to delete the instance. I may be making a mistake with my terminology, but what I mean to say is that I have a ton of "roaches" on the window and I want specific ones to disappear entirely.

我希望一个 Roach 类在达到一定数量的“饥饿”时“死亡”,但我不知道如何删除该实例。我的术语可能有误,但我的意思是我的窗户上有很多“蟑螂”,我希望特定的蟑螂完全消失。

I would show you the code, but it's quite long. I have the Roach class being appended into a Mastermind classes roach population list.

我会向您展示代码,但它很长。我将 Roach 类附加到 Mastermind 类蟑螂种群列表中。

回答by Ignacio Vazquez-Abrams

You cannot force a Python object to be deleted; it will be deleted when nothing references it (or when it's in a cycle only referred to be the items in the cycle). You will have to tell your "Mastermind" to erase its reference.

您不能强制删除 Python 对象;当没有任何内容引用它时(或者当它在一个循环中时仅被引用为循环中的项目),它将被删除。你必须告诉你的“策划者”删除它的参考。

del somemastermind.roaches[n]

回答by Robert Lujo

In general:

一般来说:

  • Each binding variable -> object increases internal object's reference counter
  • there are several usual ways to decrease reference (dereference object -> variable binding):

    1. exiting block of code where variable was declared (used for the first time)
    2. destructing object will release references of all attributes/method variable -> object references
    3. calling del variablewill also delete reference in the current context
  • after all references to one object are removed (counter==0) it becomes good candidate for garbage collection, but it is not guaranteed that it will be processed (reference here):

  • 每个绑定变量 -> 对象增加内部对象的引用计数器
  • 有几种常用的方法可以减少引用(取消引用对象 -> 变量绑定):

    1. 退出声明变量的代码块(第一次使用)
    2. 销毁对象将释放所有属性/方法变量的引用 -> 对象引用
    3. 调用del variable还将删除当前上下文中的引用
  • 在删除对一个对象的所有引用(计数器== 0)后,它成为垃圾收集的良好候选者,但不能保证它会被处理(参考此处):

CPython currently uses a reference-counting scheme with (optional) delayed detection of cyclically linked garbage, which collects most objects as soon as they become unreachable, but is not guaranteed to collect garbage containing circular references. See the documentation of the gc module for information on controlling the collection of cyclic garbage. Other implementations act differently and CPython may change. Do not depend on immediate finalization of objects when they become unreachable (ex: always close files).

CPython 当前使用带有(可选)延迟检测循环链接垃圾的引用计数方案,该方案在大多数对象变得不可访问时立即收集它们,但不能保证收集包含循环引用的垃圾。有关控制循环垃圾收集的信息,请参阅 gc 模块的文档。其他实现的行为不同,CPython 可能会改变。当对象变得无法访问时,不要依赖对象的立即终结(例如:始终关闭文件)。

回答by Beatrix Kidco

for i,roach in enumerate(roachpopulation_list)
        if roach.hunger == 100
            del roachpopulation_list[i]
            break

Remove the instance by deleting it from your population list (containing all the roach instances.

通过从人口列表(包含所有蟑螂实例)中删除它来移除实例。

If your Roaches are Sprites created in Pygame, then a simple command of .kill would remove the instance.

如果您的蟑螂是在 Pygame 中创建的精灵,那么一个简单的 .kill 命令将删除该实例。