Python:如何“杀死”一个类实例/对象? [英] Python: how to "kill" a class instance/object?

查看:121
本文介绍了Python:如何“杀死”一个类实例/对象?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想要一个Roach类在达到一定数量的饥饿时死亡,但我不知道如何删除该实例。我可能会错误地用我的术语,但我的意思是我在窗口上有很多蟑螂,我想要具体的蟑螂完全消失。

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.

我会告诉你代码,但是它很长。

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.

推荐答案

一般情况下:


  • 每个绑定变量 - > object增加内部对象的引用计数器

  • 有几种常用的方法来减少引用对象 - >变量绑定):

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


  1. 退出代码块,其中声明变量(第一次使用)

  2. 销毁对象将释放所有属性/方法变量 - >对象引用的引用

  3. 调用 del变量在所有对一个对象的引用被删除(counter == 0)之后,在当前上下文中引用

  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 variable will also delete reference in the current context


  • 垃圾回收,但不能保证会被处理(参考此处):


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

    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).




    • 对象上存在多少引用,请使用 sys.getrefcount

      用于配置/检查垃圾回收的模块 gc

      module for configure/check garbage collection is gc

      GC会调用 object .__ del__ 方法销毁对象时(其他参考此处

      GC will call object.__ del__ method when destroying object (additional reference here)

      某些不可变对象(如字符串)以特殊方式处理如果两个var包含相同的字符串,它们可能引用同一个对象,但有些不是 - 检查标识对象,为什么id(...)的返回值会更改?

      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可以通过内置函数 id 找到

      id of object can be found out with builtin function id

      模块 memory_profiler 看起来很有意思 - 用于监控内存使用的模块python程序

      module memory_profiler looks interesting - A module for monitoring memory usage of a python program

      这个主题有很多有用的资源,例如:查找对python中所有对象的引用

      there is lot of useful resources for the topic, one example: Find all references to an object in python

      这篇关于Python:如何“杀死”一个类实例/对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

  • 查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆