pythonraii

Resource Aquisition Is Initialization, in Python


I am new to Python. I come from C++.

In some code reviews, I've had several peers wanting me to move things from __init__ and __del__ to a start and stop method. Most of the time, this goes against the RAII that was beaten into my head with decades of C++.

https://en.wikipedia.org/wiki/Resource_acquisition_is_initialization

Is RAII not a thing in Python? Shouldn't it be?

After all, we can throw exceptions and we'd want to release resources when we do, no?

If it isn't. Can someone give some insight as to why things are done differently? Is there a language feature that I don't understand?

If I have:

class Poop:
    def __init__:
        # Get some Windows Resource
    def __del__:
        # Release some Windows Resource

def foo():
    poop = Poop()
    raise Exception("Poop happens")

The Windows Resource is released, right?


Solution

  • RAII works in C++ because destruction is deterministic.

    In garbage collected languages like Python, your object could theoretically never be destroyed, even if you call del on it.

    Anyway, the idiomatic way to handle resources in Python is not with RAII, nor with start/stop, but with context managers.

    The simplest example is with a file object:

    with open('this_file.txt') as f:
        #  ... do stuff with f ...
    
    # ... back to code that doesn't touch f ...
    

    The with statement is, more or less, a try-finally block that creates a resource and ensures that the resource is cleaned up when the block ends; something like this:

    try:
        f = open('this_file.txt')
        #  ... do stuff with f ...
    
    finally:
        f.close()
    
    # ... back to code that doesn't touch f ...
    

    I don't know Java, but I believe that the JVM also uses garbage collection, and similarly try-finally is an idiom for resource management in Java.

    Anyway, the with statement takes a context manager, which is an instance of a class defining the __enter__ and __exit__ methods (see the docs).

    For completeness, there may be cases where you want a context manager, but don't want to define a whole class just for that. In that case, contextlib may help.

    A worked example; say you have a resource:

    class Resource:
    
        def method(self):
            pass
    
    get_resource = Resource
    release_resource = lambda x: None
    

    A RAII-like class might look something like this:

    class RAIILike:
    
        def __init__(self):
            self.resource = get_resource()
    
        def __del__(self):
            release_resource(self.resource)
    
        def do_complex_thing(self):
            #  do something complex with resource
            pass
    
    raii_thingy = RAIILike()
    

    And you would use the resource like this:

    raii_thingy.resource.method()
    

    On the other hand, a context managed resource could look like this...

    class ContextManagedResource:
    
        def __enter__(self):
            self._resource = get_resource()
            return self._resource
    
        def __exit__(self, exc_type, exc_value, traceback):
            if exc_type is not None:
                #  handle exception here
                pass
    
            else:
                pass
    
            release_resource(self._resource)
            return True
    

    ...and be used like this:

    with ContextManagedResource() as res:
        res.method()
    

    Once the with block ends, the resource will be automatically released, regardless of whether the object that obtained it has been garbage collected.