does python have useless destructors?

Günter Jantzen nc-jantzegu at netcologne.de
Thu Jun 10 12:23:44 EDT 2004


Peter Hansen wrote:
> If you are concerned that the open might fail, which is a *different*
> type of exception, then you probably want a try/except block around
> the whole thing as well as the try/finally.

Yes this would be good. The main issue is that the finally clause works
independently of what has been done before in the try clause. The programmer
should realize this and make the finally clause as independent from the try
clause as posssible

I try too avoid too much nested exception blocks because this coding style
is sometimes difficult to understand
Sometimes I initalize my resource handlers just before the try/finally
block. And I write the finally clause so, that every resource has a chance
to be closed

--------------------------------------------------
myfile, my_db42 = None, None

try:
    myfile = open("myfilepath", "w")
    my_db42 = database.open.(connectstring)
    #
finally:
    # should work independent of what happens in the try clause !!!
    # Here I assume that close never throws an exception
    if myfile : myfile.close()
    if my_db42: my_db42.close()
---------------------------------------

regards
Günter





More information about the Python-list mailing list