473,909 Members | 2,200 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 4767
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******@impat hnetworks.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.acquir e()
try:
....
finally:
the_lock.releas e()

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.dev el 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
448
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 scope, the dtor could be immediately be called (no GC needed). For that, you don't have to declare the whole File class as a struct (which would be not good for performance when File has a lot of data-members). Instead creating a thin wrapper...
23
2309
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 boolean values or flags. Would you like that a template class will be added to implement locks or state indicators for example? Regards, Markus
9
3019
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. 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,
0
10037
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9879
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
11348
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10921
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8099
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5938
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
6140
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4776
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
4336
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.