RFC: Proposal: Deterministic Object Destruction

Chris Angelico rosuav at gmail.com
Sat Mar 3 12:43:40 EST 2018


On Sun, Mar 4, 2018 at 4:37 AM, Richard Damon <Richard at damon-family.org> wrote:
> On 3/3/18 11:33 AM, Michael Torrie wrote:
>>
>> On 03/03/2018 09:02 AM, ooomzay at gmail.com wrote:
>>>
>>> I can assure you that RAII does what it says on the tin and is relied on
>>> in
>>> many critical systems to release resources robustly ... given the
>>> pre-requisite deterministic destruction.
>>
>> Sure but did you read what Paul Moore wrote?  He said RAII works in C++
>> because objects are allocated on the *stack* with strict lifetimes and
>> scopes. They won't ever have cycles and they are guaranteed to be
>> destroyed no matter what as the stack is unwound.  Python has no
>> stack-allocated objects.
>>
>> In C++, Heap-allocated objects must still be managed manually, without
>> the benefit of RAII, for much of the same reasons as people are giving
>> here for why RAII is not a good fit for Python.  There are smart pointer
>> objects that try to give RAII semantics to heap-allocated objects, with
>> varying degrees of success. In other words there are some limitations.
>>
>> Python does not have stack-allocated objects, so the same issues that
>> prevent RAII from automatically applying in C++ to heap objects exist
>> here.
>
>
> Yes, stack allocated object in C++ have a nice lifetime to allow RAII to
> work, but it doesn't just work with stack allocated objects. A lot of RAII
> objects are members of a class object that may well be allocated on the
> heap, and RAII makes sure that all the needed cleanup gets done when that
> object gets destroyed.

How do you guarantee that the heap object is properly disposed of when
you're done with it? Your RAII object depends 100% on the destruction
of the heap object.

ChrisA



More information about the Python-list mailing list