473,407 Members | 2,359 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,407 software developers and data experts.

Is RAII possible in Python?

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

Jul 18 '05 #1
4 4738
Pierre Rouleau wrote:
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.
--
Rainer Deyke - ra*****@eldwood.com - http://eldwood.com
Jul 18 '05 #2
Pierre Rouleau <pr******@impathnetworks.com> writes:
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.
Jul 18 '05 #3
You need to use the try/finally statement or see PEP 310.
http://www.python.org/peps/pep-0310.html
Jul 18 '05 #4


Pierre Rouleau wrote:
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

Jul 18 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

26
by: codymanix | last post by:
Last night I had several thought about RAII and want to discuss a bit. Why doesn't CSharp support destructors in structs? Wouldn't that make RAII possible like in C++? When the struct goes out of...
23
by: Markus Elfring | last post by:
The class "auto_ptr" implements the RAII pattern for pointer types. It seems that an implementation is not provided for non-pointer values by the STL so far. I imagine to use the "acquisition" for...
9
by: plahey | last post by:
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. ...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.