Is RAII possible in Python?

Discussion in 'Python' started by Pierre Rouleau, Nov 15, 2003.

  1. 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
    Pierre Rouleau, Nov 15, 2003
    #1
    1. Advertising

  2. Pierre Rouleau

    Rainer Deyke Guest

    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 - - http://eldwood.com
    Rainer Deyke, Nov 15, 2003
    #2
    1. Advertising

  3. Pierre Rouleau

    Paul Rubin Guest

    Pierre Rouleau <> 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.
    Paul Rubin, Nov 15, 2003
    #3
  4. Pierre Rouleau

    MetalOne Guest

    MetalOne, Nov 16, 2003
    #4
  5. 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
    Pierre Rouleau, Nov 18, 2003
    #5
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. MikeB
    Replies:
    4
    Views:
    350
    Owen Jacobson
    Oct 26, 2004
  2. Replies:
    9
    Views:
    665
    Carl Friedrich Bolz
    Feb 5, 2006
  3. Johannes Schaub (litb)

    Re: Why is RAII called RAII?

    Johannes Schaub (litb), Sep 12, 2010, in forum: C++
    Replies:
    2
    Views:
    401
    James Kanze
    Sep 18, 2010
  4. cpp4ever

    Re: Why is RAII called RAII?

    cpp4ever, Sep 12, 2010, in forum: C++
    Replies:
    1
    Views:
    408
    BGB / cr88192
    Sep 13, 2010
  5. Goran Pusic

    Re: Why is RAII called RAII?

    Goran Pusic, Sep 13, 2010, in forum: C++
    Replies:
    11
    Views:
    553
    ptyxs
    Sep 16, 2010
Loading...

Share This Page