How to delete every reference of an object in Python?(如何在 Python 中删除对象的每个引用?)
问题描述
假设你有类似的东西:
x = "something"
b = x
l = [b]
如何删除只有一个引用的对象,比如 x?
How can you delete the object only having one reference, say x?
del x
不会成功;例如,该对象仍然可以从 b 访问.
del x
won't do the trick; the object is still reachable from b, for example.
推荐答案
不不不.Python 有一个垃圾收集器,它有非常严重的领域问题——它不会干扰你创建对象,也不会干扰它删除对象.
No no no. Python has a garbage collector that has very strong territory issues - it won't mess with you creating objects, you don't mess with it deleting objects.
简而言之,这是不可能的,而且是有充分理由的.
Simply put, it can't be done, and for a good reason.
例如,如果您的需求来自例如缓存算法保留引用的情况,但不应阻止数据在没有人使用时被垃圾收集,您可能想看看 weakref
.
If, for instance, your need comes from cases of, say, caching algorithms that keep references, but should not prevent data from being garbage collected once no one is using it, you might want to take a look at weakref
.
这篇关于如何在 Python 中删除对象的每个引用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如何在 Python 中删除对象的每个引用?
基础教程推荐
- 何时使用 os.name、sys.platform 或 platform.system? 2022-01-01
- 线程时出现 msgbox 错误,GUI 块 2022-01-01
- Dask.array.套用_沿_轴:由于额外的元素([1]),使用dask.array的每一行作为另一个函数的输入失败 2022-01-01
- 用于分类数据的跳跃记号标签 2022-01-01
- 如何在海运重新绘制中自定义标题和y标签 2022-01-01
- Python kivy 入口点 inflateRest2 无法定位 libpng16-16.dll 2022-01-01
- 如何让 python 脚本监听来自另一个脚本的输入 2022-01-01
- 在 Python 中,如果我在一个“with"中返回.块,文件还会关闭吗? 2022-01-01
- 筛选NumPy数组 2022-01-01
- 使用PyInstaller后在Windows中打开可执行文件时出错 2022-01-01