Need a little parse help

Peter Hansen peter at engcorp.com
Tue May 10 21:24:45 EDT 2005


Mike Meyer wrote:
> I'd like to note that failing to close the file explicitly is a bad
> habit. You really should invoke the close method, rather than relying
> on the garbage collector to close them for you. This means you do need
> a variable to hold the file object. 99% of the time nothing bad will
> happen if you skip this, but that 1% can always come back to byte you
> in the *ss.

While that's generally true, it's worth noting that for small utility 
scripts of the "throw-away" variety, adding in the code necessary to do 
properly what Mike describes can make the difference between the code 
being quick and simple and (at least for those fairly new to Python) 
awkward and confusing, and thus potentially buggy.

In my opinion, if the code fits on one screen and just reads stuff from 
one file and, maybe, writes to another, you can safely and with clean 
conscience ignore Mike's advice (but remember it for later!).

-Peter



More information about the Python-list mailing list