Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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 ?

like image 717
Ryan Ye Avatar asked Oct 10 '11 10:10

Ryan Ye


People also ask

What is __ Weakref __ in Python?

The weakref module allows the Python programmer to create weak references to objects. In the following, the term referent means the object which is referred to by a weak reference.

What is WeakValueDictionary?

A WeakValueDictionary d is a mapping that references its values weakly. When the reference count of a value v in d goes to 0 , all items of d such that d [ k ] is v disappear. adict is used to initialize the mapping.


1 Answers

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.

like image 159
ekhumoro Avatar answered Sep 28 '22 10:09

ekhumoro