Issue7170
Created on 2009-10-19 21:48 by stutzbach, last changed 2009-10-21 07:18 by georg.brandl.
|
msg94260 - (view) |
Author: Daniel Stutzbach (stutzbach) |
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 "<stdin>", line 1, in <module>
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) |
Date: 2009-10-20 03:00 |
|
That's why it says "several" builtin types.
|
|
msg94266 - (view) |
Author: Daniel Stutzbach (stutzbach) |
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) |
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) |
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) |
Date: 2009-10-21 07:18 |
|
Fixed in r75580, r75581.
|
|
| Date |
User |
Action |
Args |
| 2009-10-21 07:18:06 | georg.brandl | set | status: open -> closed resolution: fixed messages:
+ msg94308
|
| 2009-10-20 20:42:28 | stutzbach | set | messages:
+ msg94298 |
| 2009-10-20 03:22:54 | benjamin.peterson | set | nosy:
+ georg.brandl messages:
+ msg94267
assignee: georg.brandl components:
+ Documentation, - Interpreter Core |
| 2009-10-20 03:14:57 | stutzbach | set | messages:
+ msg94266 |
| 2009-10-20 03:00:08 | benjamin.peterson | set | nosy:
+ benjamin.peterson messages:
+ msg94265
|
| 2009-10-19 21:48:07 | stutzbach | create | |
|