[Python-ideas] Why not break cycles with one __del__?

Scott Dial scott+python-ideas at scottdial.com
Tue Sep 14 07:12:37 CEST 2010

On 9/13/2010 11:07 PM, Guido van Rossum wrote:
> On Mon, Sep 13, 2010 at 8:04 PM, Tim Peters <tim.peters at gmail.com> wrote:
>> [Nick Coghlan]
>>>> Alternatively, when building finalizers, build two
>>>> lists: one for objects with __del__ methods and one for objects that
>>>> are reachable from objects with __del__ methods.
>> [Greg Ewing]
>>> But since it's a cycle, isn't *everything* in the cycle
>>> going to be reachable from everything else?
>> That pattern is exploitable too.  If, e.g., you have some resource R
>> that needs to be cleaned up, owned by an object A that may participate
>> in cycles, it's often possible to put R in a different, very simple
>> object with a __del__ method, and have A point to that latter object
>> instead.
> Yeah, I think we even recommended this pattern at some point. ISTR we
> designed the new io library to exploit it.

Yes, this topic came up some while back on this list and Tim's solution
is exactly the design pattern I suggested then:


Scott Dial
scott at scottdial.com
scodial at cs.indiana.edu

More information about the Python-ideas mailing list