Issue 7170: subclasses of (some) built-in types are not weakref-able (original) (raw)

Created on 2009-10-19 21:48 by stutzbach, last changed 2022-04-11 14:56 by admin. This issue is now closed.

Messages (6)
msg94260 - (view) Author: Daniel Stutzbach (stutzbach) (Python committer) Date: 2009-10-19 21:48
The documentation for weakref contains the following example: http://docs.python.org/3.1/library/weakref.html ---------------------------------------------- Several built-in types such as list and dict do not directly support weak references but can add support through subclassing: class Dict(dict): pass obj = Dict(red=1, green=2, blue=3) # this object is weak referenceable ---------------------------------------------- While this works fine for list and dict, it does not work for tuple or int: >>> class Tuple(tuple): ... pass ... >>> obj = Tuple() >>> weakref.ref(obj) Traceback (most recent call last): File "", line 1, in TypeError: cannot create weak reference to 'Tuple' object I've tried it in Python 2.5, 2.6, and 3.1.
msg94265 - (view) Author: Benjamin Peterson (benjamin.peterson) * (Python committer) Date: 2009-10-20 03:00
That's why it says "several" builtin types.
msg94266 - (view) Author: Daniel Stutzbach (stutzbach) (Python committer) Date: 2009-10-20 03:14
I parsed the documentation this way: "Several built-in types such as list and dict do not directly support weak references" (true) "but [all of those that do not directly support weak references] can add support through subclassing" I would expect there to be exactly two groups of items: - types that support weak references directly - types where it can be added through subclassing Is there some technical reason why int, tuple, and others cannot support it through subclassing?
msg94267 - (view) Author: Benjamin Peterson (benjamin.peterson) * (Python committer) Date: 2009-10-20 03:22
The documentation should be rewritten for clarity then. The reason you can't take weakrefs of those types is because they are implemented as "varsized" objects, so there is no constant place to add a weakref list in the memory layout of the objects.
msg94298 - (view) Author: Daniel Stutzbach (stutzbach) (Python committer) Date: 2009-10-20 20:42
That's fair. I suggest the following change: current text: "Several built-in types such as list and dict do not directly support weak references but can add support through subclassing:" new text: "Several built-in types such as list and dict do not directly support weak references but can add support through subclassing, as shown below. Other built-in types such as tuple and int do not support weak references even when subclassed."
msg94308 - (view) Author: Georg Brandl (georg.brandl) * (Python committer) Date: 2009-10-21 07:18
Fixed in r75580, r75581.
History
Date User Action Args
2022-04-11 14:56:54 admin set github: 51419
2009-10-21 07🔞06 georg.brandl set status: open -> closedresolution: fixedmessages: +
2009-10-20 20:42:28 stutzbach set messages: +
2009-10-20 03:22:54 benjamin.peterson set nosy: + georg.brandlmessages: + assignee: georg.brandlcomponents: + Documentation, - Interpreter Core
2009-10-20 03:14:57 stutzbach set messages: +
2009-10-20 03:00:08 benjamin.peterson set nosy: + benjamin.petersonmessages: +
2009-10-19 21:48:07 stutzbach create