Deterministic destruction and RAII idioms in Python



I have been dabbling in Python for a while now. One of the things that
really appeals to me is that I can seem to be able to use C++-style
RAII idioms to deal with resource management issues.

For those that have no idea what I am talking about (I learn a lot
reading posts on subjects in which I am clueless), consider the
following code snippet:

for line in file(name):
...print line,

This is nice and clean because I don't have to worry about cleaning
up after myself. If I can't rely on the destructor for the file
object to close the file, I must write the code like this:

file_obj = file(name)
for line in file_obj:
...print line,
file_obj.close()

not nearly as nice. Depending on the type of work you do, this can be
no problem or a major headache when exceptions are thrown into the mix.

Python objects have destructors so it seems that the original intent
was to support deterministic destruction (destructors are not very
useful in most GC'ed languages since you never know when or even if
they will be called). The problem is that other implementations of
Python (Jython and IronPython) do not support deterministic
destruction. So we are left with a problem: is deterministic
destruction an implementation detail of CPython that can go away at
anytime, or is it an official property of the language.

Giving up deterministic destruction in Python would be a real blow for
me, since it is one of its unique features among GC'ed languages.

So what's the deal, can I rely on it in "mainstream" Python or am
I out of luck here?

.



Relevant Pages

  • Re: Deterministic destruction and RAII idioms in Python
    ... > Python do not support deterministic ... > destruction an implementation detail of CPython that can go away at ... or is it an official property of the language. ... Depends on what you mean by 'mainstream'. ...
    (comp.lang.python)
  • Re: Deterministic destruction and RAII idioms in Python
    ... > Giving up deterministic destruction in Python would be a real blow for ... can I rely on it in "mainstream" Python or am ... other references when the scope exits. ...
    (comp.lang.python)
  • Re: Python C-API Object Allocation
    ... tp_new, tp_init and tp_alloc for creation and tp_del, tp_free and tp_dealloc for destruction. ... Which of the python tp_* methods do I need to provide and what must they do to be compatible with python subclassing. ... there are lists specifically for the Python C API and C++ issues. ... I don't mean to chase you away from here; plenty of people ask C API questions here. ...
    (comp.lang.python)
  • Re: Newbie question about string(passing by ref)
    ... new string with the same value from it. ... This, of course, is an implementation detail. ... languages which use "interned" objects and, as far as I know, they are ... (An example, in Python, of an object with a defined identity would be ...
    (comp.lang.python)
  • Re: Basic inheritance question
    ... Old Java habits die slowly. ... No, seriously it isn't Java habits only, most other languages wouldn't ... That's not very far from what a Python method object does - ... reference to the current instance is to pass it as an argument to the ...
    (comp.lang.python)