Is RAII possible in Python?

P

Pierre Rouleau

As much as I love Python, I sometimes find myself wishing Python
supported the RAII idiom (resource acquisition is initialization) that
is available in C++, the emerging D language, and others.

In some situations (like controlling non-memory resources) it is nice to
be able to create an object that will execute some code on its
destruction. For example, an object that controls access to a critical
section: the creation of the object calls the function required to enter
the critical section, and the __del__() calls the function required to
exit the critical section. If an exception occurs while the code is
insinde the critical section managed by the object, the the object's
__del__() is automatically called and the critical section exited.

AFAIK, the call of __del__() method on object destruction is not
garanteed to be called when the interpreter exists. Is this true?

Is the __del__() mehod of an object garanteed to be called when a
function exists normally or is forced to exit by an exception?

Is RAII available in Python?

Thanks

Pierre
 
R

Rainer Deyke

Pierre said:
Is RAII available in Python?

In practice, yes. Objects are destroyed when their reference count goes to
zero.

In theory, no. The language specification does not guarantee this behavior.
 
P

Paul Rubin

Pierre Rouleau said:
AFAIK, the call of __del__() method on object destruction is not
garanteed to be called when the interpreter exists. Is this true?

I think it is supposed to be called.
Is the __del__() mehod of an object garanteed to be called when a
function exists normally or is forced to exit by an exception?
No.

Is RAII available in Python?

You can use the try/finally construction to make sure the object gets
destroyed when the finally clause runs.
 
P

Pierre Rouleau

Pierre said:
As much as I love Python, I sometimes find myself wishing Python
supported the RAII idiom (resource acquisition is initialization) that
is available in C++, the emerging D language, and others.

In some situations (like controlling non-memory resources) it is nice to
be able to create an object that will execute some code on its
destruction. For example, an object that controls access to a critical
section: the creation of the object calls the function required to enter
the critical section, and the __del__() calls the function required to
exit the critical section. If an exception occurs while the code is
insinde the critical section managed by the object, the the object's
__del__() is automatically called and the critical section exited.

AFAIK, the call of __del__() method on object destruction is not
garanteed to be called when the interpreter exists. Is this true?

Is the __del__() mehod of an object garanteed to be called when a
function exists normally or is forced to exit by an exception?

Is RAII available in Python?

Thanks to all that posted a reply.

To summarize those replies, in Python 2.3 the 'calling' code is
responsible to provide protection with the try/finally clause, as in
(atken from pep 310):

the_lock.acquire()
try:
....
finally:
the_lock.release()

PEP-310 (http://www.python.org/peps/pep-0310.html) proposes a more
condensed syntax using a new keyword (with).

I have seen some PEP310 discussion on comp.python.devel and I hope
PEP310 will be implemented in the next version of Python (2.4) as
written on the PEP-310 page. Where can we find out about the state of a
PEP and whether it's going to get implemented?


Thanks!

Pierre
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,014
Latest member
BiancaFix3

Latest Threads

Top