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
Python: how to "kill" a class instance/object?
提问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):
- exiting block of code where variable was declared (used for the first time)
- destructing object will release references of all attributes/method variable -> object references
- 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):
- 每个绑定变量 -> 对象增加内部对象的引用计数器
有几种常用的方法可以减少引用(取消引用对象 -> 变量绑定):
- 退出声明变量的代码块(第一次使用)
- 销毁对象将释放所有属性/方法变量的引用 -> 对象引用
- 调用
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 可能会改变。当对象变得无法访问时,不要依赖对象的立即终结(例如:始终关闭文件)。
how many references on the object exists, use sys.getrefcount
module for configure/check garbage collection is gc
GC will call object.__ del__method when destroying object (additional reference here)
some immutable objects like strings are handled in a special way - e.g. if two vars contain same string, it is possible that they reference the same object, but some not - check identifying objects, why does the returned value from id(...) change?
id of object can be found out with builtin function id
module memory_profilerlooks interesting - A module for monitoring memory usage of a python program
there is lot of useful resources for the topic, one example: Find all references to an object in python
对象上存在多少引用,使用sys.getrefcount
用于配置/检查垃圾收集的模块是gc
GC销毁对象时会调用object.__ del__方法(这里补充参考)
某些不可变对象(如字符串)以特殊方式处理 - 例如,如果两个变量包含相同的字符串,则它们可能引用相同的对象,但有些则不是 - 检查标识对象,为什么从 id(...) 返回的值改变?
模块memory_profiler看起来很有趣 -一个用于监控 python 程序内存使用情况的模块
这个主题有很多有用的资源,一个例子:在 python 中查找对一个对象的所有引用
回答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 命令将删除该实例。

