does python have useless destructors?

Roger Binns rogerb at rogerbinns.com
Thu Jun 10 17:21:10 EDT 2004


Duncan Booth wrote:
> def f(filename):
>    dispose infile, outfile
>
>    infile = file(filename, "r")
>    outfile = file(filename+".out", "w")
>    outfile.write(infile.read())

While that looks nice, the real problem I have with it is that
the caller/consumer of objects has to know that they need
disposing.  For example they have to know that strings don't
need to be disposed of but files do.  And then things get
really complicated with toolkits.  For example in a graphical
toolkit do windows, device contexts, colours, brushes etc
have to be disposed of?

The correct answer of course is that the object itself
should be aware that it needs to be disposed of and that
real world resources can leak if it isn't.

Which brings us back in a full loop.  Currently objects
signify that they need disposing by having a destructor.
Why not "fix" that mechanism?

Roger





More information about the Python-list mailing list