<div class="gmail_quote">On Thu, May 17, 2012 at 8:44 AM, Antoine Pitrou <span dir="ltr"><<a href="mailto:solipsis@pitrou.net" target="_blank">solipsis@pitrou.net</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">

<div class="im">On Thu, 17 May 2012 08:10:40 -0700<br>
Ethan Furman <<a href="mailto:ethan@stoneleaf.us">ethan@stoneleaf.us</a>> wrote:<br>
>  From the manual [8.11]:<br>
><br>
>  > A weak reference to an object is not enough to keep the object alive:<br>
>  > when the only remaining references to a referent are weak references,<br>
>  > garbage collection is free to destroy the referent and reuse its<br>
>  > memory for something else.<br>
><br>
> This leads to a difference in behaviour between CPython and the other<br>
> implementations:  CPython will (currently) immediately destroy any<br>
> objects that only have weak references to them with the result that<br>
> trying to access said object will require making a new one;<br>
<br>
</div>This is only true if the object isn't caught in a reference cycle.</blockquote><div><br></div><div>To further this, consider the following example, ran in CPython2.6:<br><br></div><div><div>>>> import weakref</div>

<div>>>> import gc</div><div>>>></div><div>>>> class O(object):</div><div>...     pass</div><div>...</div><div>>>> a = O()</div><div>>>> b = O()</div><div>>>> a.x = b</div>

<div>>>> b.x = a</div><div>>>></div><div>>>> w = weakref.ref(a)</div><div>>>></div><div>>>></div><div>>>> del a, b</div><div>>>></div><div>>>> print w()</div>

<div><__main__.O object at 0x0000000003C78B38></div><div>>>></div><div>>>> gc.collect()</div><div>20</div><div>>>></div><div>>>> print w()</div><div>None</div></div><div> </div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">

<div class="HOEnZb"><div class="h5">
<br>
_______________________________________________<br>
Python-ideas mailing list<br>
<a href="mailto:Python-ideas@python.org">Python-ideas@python.org</a><br>
<a href="http://mail.python.org/mailman/listinfo/python-ideas" target="_blank">http://mail.python.org/mailman/listinfo/python-ideas</a><br>
</div></div></blockquote></div><br>