Destructors and exceptions

Konstantin Veretennicov kveretennicov at
Wed Jun 9 13:20:41 CEST 2004

"Humpty Dumpty" <oliver.schoenborn at> wrote in message news:<Ysaxc.19793$sS2.682217 at>...
> There is no Python equivalent of C++'s "destructor garanteed to be called
> upon scope exit", for a couple of reasons: scope exit only destroys
> references to objects, not the objects themselves; destruction of objects is
> left to the garbage collector and you have no influence on it. In
> particular, the gc is not required to release resources, so finalizers (the
> __del__ method, closest to C++'s destructor) may not get called. This means
> __del__ is pretty much useless (AFAIMC), and you can't rely on them being
> called before program exit (or ever, for that matter).
> I agree, it is a real pitty that Python doesn't have a way of doing what you
> mention, other than try-finally, which makes code more difficult to read. A
> new specifier, e.g. "scoped", would be a required addtion to Python:
> interpreter would garantee that __del__ of scoped objects would be called on
> scope exit, and raise an exception if attempt to alias.

Is there a PEP or something for "scoped specifier"?

- kv

More information about the Python-list mailing list