Getting happier ;-), but wondering if I'm thinking pythonically

Delaney, Timothy C (Timothy) tdelaney at avaya.com
Wed May 21 00:57:12 EDT 2003


> From: Brian Quinlan [mailto:brian at sweetapp.com]
> 
> Timothy Delaney wrote:
> > In CPython. Except that if you don't explicitly close files that you
> are
> > *writing* then you may find that data isn't flushed.
> 
> How so? Unless the file object is participating in a cyclic data
> structure, when will it not be collected?

Any open file at program exit will not be closed by Python. Python will quit without running finalisers.

If you have unflushed data waiting to be written to a file at that point, you may well find that the file is closed by the OS at that point, and the unflushed data is not written.

This is more prevalent in Jython than CPython, but it's possible in both.

This is the same as any resource. You cannot rely on Python to clean up after you at program exit, and after that you're at the mercy of the OS.

> > Get used to explicitly closing files - and do it in a 
> finally: block!
> 
> No.

Why not? I know you object to explicitly closing files ... do you also object to using finally blocks?
 
> > > the decision to exit an application should be made close to the
> > > top level (this is true of other languages with exception 
> handling)
> > > (e.g. line 52)
> 
> > Not necessarily. If there is a fatal condition, choosing to exit at
> that
> > point is fine. 
> 
> How does library code know what constitutes a "fatal condition"? 

Library code should throw an exception. Non-library code may choose to do what is appropriate. Where the line between library code and non-library code is undetermined at this point.

> > > 5. you should generate more descriptive error messages (e.g. line
> 30)
> > 
> > This is related to my comments for point 4. A stack trace 
> is much more
> > descriptive ;)
> 
> But probably scary and useless to the user. 

Only if it's *seen* by the user. OTOH, if you have a catch-all at the topmost level of the application, that writes the exception to a log file and displays an error to the user then it's not particularly scary. Of course, such a displayed error should let the user know where the logged info is so that a knowledgable user can give you useful feedback :)

Tim Delaney





More information about the Python-list mailing list