RFC: Proposal: Deterministic Object Destruction

Richard Damon Richard at Damon-Family.org
Sat Mar 3 19:30:59 EST 2018


On 3/3/18 6:49 PM, Steven D'Aprano wrote:
> On Sat, 03 Mar 2018 12:37:08 -0500, Richard Damon wrote:
>
>> With RAII and immediate destruction on end of scope, we can automate the
>> release, without it and you need a lot of explicit code to manage these
>> resources.
> Not so much.
>
> with resource_i_care_about() as rsrc:
>      process(rsrc)
>
>
> is hardly what I call "a lot of explicit code". Yes, there's some code in
> the context manager, but you write that *once* (if it isn't already
> written for you!) and you're done.

The with statement handles the C++ case of using a unique_ptr, where the 
resource is aquired and release within a single block scope and thus a 
single stack based object is ultimately responsible for the lifetime of 
the resource.

I am not sure how it would handle a resource that gets shared for a 
while with something like a shared_ptr, and needs to be released when 
all the people who is has been shared with are done with their copy. 
Admittedly, this case is much less likely to be using such a critical 
resource, but it might. In Python, currently, you would either need to 
keep an explicit usage count to allow you to delete it when done, or let 
things wait for the collector to find it.

-- 
Richard Damon




More information about the Python-list mailing list