如何在Python中删除对象的每个引用? [英] How to delete every reference of an object in Python?

查看:161
本文介绍了如何在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.

简而言之,

例如,如果您的需求来自保留引用的缓存算法,但应该不能阻止没有人使用过的数据被垃圾回收,您可能想看看 弱引用

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中删除对象的每个引用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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