简体   繁体   English

为什么weakref 不支持Python 中的内置类型?

[英]Why weakref doesn't support built-in types in Python?

In Python weakref document( http://docs.python.org/library/weakref.html ), it says that在 Python 弱引用文档( http://docs.python.org/library/weakref.html )中,它说

Several built-in types such as list and dict do not directly support weak references but can add support through subclassing一些内置类型如 list 和 dict 不直接支持弱引用,但可以通过子类化添加支持

I think creating weakref for big dict could be useful in some real cases.我认为为 big dict 创建 weakref 在某些实际情况下可能很有用。 I'm wondering what's the reason behind that implementation ?我想知道该实现背后的原因是什么?

Most of the built-in types are not directly weak referenceable (eg str, int, float, list, dict, None), and there are a few that cannot even be made so by sub-classing (eg tuples in CPython).大多数内置类型不能直接弱引用(例如 str、int、float、list、dict、None),还有一些甚至不能通过子类化(例如 CPython 中的元组)来实现。

Some details about the underlying implementation of weakrefs for several built-in types can be found in this March-2005 python-list post by Raymond Hettinger .可以在Raymond Hettinger 于 2005 年 3 月发表的这篇 python-list 帖子中找到有关几种内置类型的弱引用底层实现的一些详细信息。

我有根据的猜测是,内部使用 dicts 和列表来实现弱引用,所以你会在这里遇到鸡蛋鸡的情况。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM