On Thu, May 17, 2012 at 8:44 AM, Antoine Pitrou <solipsis@pitrou.net> wrote:
On Thu, 17 May 2012 08:10:40 -0700
Ethan Furman <ethan@stoneleaf.us> wrote:
>  From the manual [8.11]:
>
>  > A weak reference to an object is not enough to keep the object alive:
>  > when the only remaining references to a referent are weak references,
>  > garbage collection is free to destroy the referent and reuse its
>  > memory for something else.
>
> This leads to a difference in behaviour between CPython and the other
> implementations:  CPython will (currently) immediately destroy any
> objects that only have weak references to them with the result that
> trying to access said object will require making a new one;

This is only true if the object isn't caught in a reference cycle.

To further this, consider the following example, ran in CPython2.6:

>>> import weakref
>>> import gc
>>>
>>> class O(object):
...     pass
...
>>> a = O()
>>> b = O()
>>> a.x = b
>>> b.x = a
>>>
>>> w = weakref.ref(a)
>>>
>>>
>>> del a, b
>>>
>>> print w()
<__main__.O object at 0x0000000003C78B38>
>>>
>>> gc.collect()
20
>>>
>>> print w()
None
 

_______________________________________________
Python-ideas mailing list
Python-ideas@python.org
http://mail.python.org/mailman/listinfo/python-ideas