does python have useless destructors?

Donn Cave donn at drizzle.com
Thu Jun 10 02:46:40 EDT 2004


Quoth "Michael P. Soulier" <msoulier at digitaltorque.ca._nospam>:
...
| However, consider the code...
|
| myfile = open("myfilepath", "w")
| myfile.write(reallybigbuffer)
| myfile.close()
|
| If the write fails due to a lack of disk space, the exception will
| prevent the explicit close() from happening. Now, if myfile goes out of
| scope, I would hope that the file object is destroyed and thus the file
| is closed, but after reading the python documentation, it seems that
| the only way to guarantee closure of the file is using the mentioned
| try/finally construct...
|
| try:
|     myfile = open("myfilepath", "w")
|     myfile.write(reallybigbuffer)
| finally:
|     myfile.close()

I think here we're talking about the problem where an exception takes
a reference to local variables.  The other solution, a bit of a hack
but it could be more convenient depending on your application, is to
release the traceback (sys.exc_traceback = None) in the handler.

	Donn Cave, donn at drizzle.com



More information about the Python-list mailing list