destructor/finalizer issues in C# (deja vu for Python)

Alex Martelli aleaxit at yahoo.com
Thu Nov 16 08:53:09 EST 2000


"Chris Ryland" <cpr at emsoftware.com> wrote in message
news:10CQ5.316$gp_.35324238 at news.randori.com...
> See http://msdn.microsoft.com/voices/deepc10192000.asp, where it's
> interesting to see the C# world wrestle with the same problems with which
> Python users have struggled.

Yes, and Bobby Schmidt always writes clearly and with good
understanding (IMHO) -- see, for example, his excellent idea
of calling C#'s "destructors" "finalizers" instead, since
their semantics are so far from what C++ programmers think
of as "destructors" (calling our own __del__ a 'finalizer'
might also be similarly smart:-).

It's also interesting that his recommendation is to "consider
registering objects with an agent that will automatically
call Dispose for you in proper LIFO order" -- which is
basically the same approach that I suggested for Python
(at about the same time as he was writing this article --
and I'm pretty sure we weren't reading each other's stuff:-).

The Java community has wrestled with these issues for a
long while, of course.  While the detailed mechanisms each
language gives the programmer for controlling finalization
differ (and in Python's case depend on the exact version --
CPython, Jython, .NET), the general ideas of using an
explicitly-named 'Dispose' method (rather than relying on
the uncertain finalizer itself), delegating finalization
to a helper-object, and relying on try/finally (which all
of these languages have), are pretty interchangeable.


Alex






More information about the Python-list mailing list