RFC: Proposal: Deterministic Object Destruction

ooomzay at gmail.com ooomzay at gmail.com
Wed Feb 28 17:51:09 EST 2018


Informal Background
===================

Python's lack of Deterministic Object Destruction semantics strikes me as very unpythonic. This state of affairs spawns lengthy diatribes on __del__(), a variety of specialised and onerous resource management solutions and malignant carbuncles such as PEP 343 (the "with" statement). None of these match the practical utility, simplicity, generality and robustness of Deterministic Object Destruction combined with the RAII idiom to manage resources.

I have therefore drafted the radical but simple and 100% backward compatible proposal below to remedy this situation and pave the way for numerous tidy ups (out of scope of this proposal).

I am now retiring to a safe distance and invite your comments on the PEP below:-


Abstract
========

This PEP proposes that valid python interpreters *must* synchronously destroy objects when the last reference to an object goes out of scope. This interpreter behaviour is currently permitted and exhibited by the reference implementation [CPython], but it is optional.

Motivation
==========

To make the elegance (simplicity, generality, and robustness) of the "Resource Acquisition Is Initialization" (RAII) idiom available to all python applications and libraries.

To render less elegant and more onerous resource management solutions, such as the "with" Statement [PEP 343] obsolete.

Specification
=============

When the last reference to an object goes out of scope the intepreter must synchronously, in the thread that releases the last reference, invoke the object's __del__() method and then free the memory occupied by that object.

Backwards Compatibility
=======================

This proposal preserves 100% backwards compatibility.

The language currently does not prescribe when, or even if, __del()__ will be called and object memory freed. Existing python applications and libraries can make no assumptions about this behaviour.

Reference Implementation
========================

CPython

References
==========

PEP 343 -- The "with" Statement



More information about the Python-list mailing list