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

浪尽此生 提交于 2020-05-23 05:53:08

问题


In Python weakref document( http://docs.python.org/library/weakref.html ), it says that

Several built-in types such as list and dict do not directly support weak references but can add support through subclassing

I think creating weakref for big dict could be useful in some real cases. I'm wondering what's the reason behind that implementation ?


回答1:


Most of the built-in types are not directly weak referenceable (e.g. str, int, float, list, dict, None), and there are a few that cannot even be made so by sub-classing (e.g. tuples in 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.




回答2:


My educated guess is that dicts and lists are used internally to implement weakrefs, so you would have an egg-chicken situation here.



来源:https://stackoverflow.com/questions/7711246/why-weakref-doesnt-support-built-in-types-in-python

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!