Resource Aquisition Is Initialization, in Python

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.

Tags:

Python

Raii