Deterministic destruction and RAII idioms in Python

Paul Rubin http
Tue Jan 31 04:15:26 CET 2006

plahey at writes:
> >> I looked at pep-0343, it looks interesting.  It is not what I really
> >> want (deterministic destruction)
> >I think it's better.
> Is there something specific you have in mind that makes you say that?...
> Which, looks like you have a constructor (__enter__) and a destructor
> (__exit__), which you can always count on being called.  I am not sure
> how that is better than C++-style RAII.  

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.

PEP 343 is closer to C++-style RAII.  I haven't studied it enough to
say what the differences really are.  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.

So to be clear, I think PEP 343 is better than simply expecting the
CPython reference counting system take care of releasing things when
the scope exits.  I'm not much of a C++ user and I don't claim
anything about PEP 343 vs C++.

More information about the Python-list mailing list