Deterministic destruction and RAII idioms in Python

plahey at plahey at
Tue Jan 31 04:56:19 CET 2006

Hi Paul,

>  I didn't understand then.  I thought by "deterministic destruction"
>  you meant relying on what CPython does now, which is reference
>  counting with destruction when the last reference is released.

I effectively do mean that.  In C++ it is guaranteed that a destructor
will be called on an object when it goes out of scope, no matter how
that happens (normal exit, exception, etc...).  C++-style RAII just
refers to wrapping up resources in classes so that you don't need to
worry about explicitly freeing them.

> I do remember thinking C++ RAII
> was a nuisance because you had to create classes with destructors to
> do what other languages did with try/finally or the equivalent.

It really depends how often you have to deal with getting/releasing
resouces.  You can define a class once and then just deal the resource
in your code with a single line (creating the object).  It can also
simplify things when you are dealing with the simultaneous management
of multiple resources.  In any case, this is now getting off topic.

Thanks for your input!  It was fun.

More information about the Python-list mailing list