Searching equivalent to C++ RAII or deterministic destructors

Roel Schroeven rschroev_nospam_ml at fastmail.fm
Thu Jul 2 17:28:19 EDT 2009


Peter Otten schreef:
> Ulrich Eckhardt wrote:
> 
>> Bearophile wrote:
>>> Ulrich Eckhardt:
>>>> a way to automatically release the resource, something
>>>> which I would do in the destructor in C++.
>>> Is this helpful?
>>> http://effbot.org/pyref/with.htm
>> Yes, it aims in the same direction. However, I'm not sure this applies to
>> my case. The point is that the resource handle is not just used locally in
>> a restricted scope but it is allocated and stored. The 'with' is something
>> that makes sense in the context of mutex locking, where you have a
>> well-defined critical section. 
> 
> Isn't that exactly what RAII does?

RAII also works if the resource handle is stored, for example, in a data
member of an object. If that object is destroyed (because it goes out of
scope, or because it is deleted), the resource is automatically
destroyed too.

The way RAII works is actually the one thing from C++ that I miss in Python.

-- 
The saddest aspect of life right now is that science gathers knowledge
faster than society gathers wisdom.
  -- Isaac Asimov

Roel Schroeven



More information about the Python-list mailing list