File not closed on exception

Gabriel Genellina gagsl-py2 at yahoo.com.ar
Tue Oct 20 03:40:24 EDT 2009


En Tue, 20 Oct 2009 03:23:49 -0300, arve.knudsen at gmail.com  
<arve.knudsen at gmail.com> escribió:
> On Oct 19, 5:56 pm, "Gabriel Genellina" <gagsl-... at yahoo.com.ar>
> wrote:
>> En Mon, 19 Oct 2009 09:45:49 -0200, arve.knud... at gmail.com  
>> <arve.knud... at gmail.com> escribió:
>>
>> > I thought that file objects were supposed to be garbage-collected and
>> > automatically closed once they go out of scope, at least that's what
>> > I've been told by more merited Python programmers.
>>
>> When an object holds references to external resources that must be  
>> freed,  
>> this is not a good idea. Being explicit with the resource deallocation  
>> is  
>> much better than relying on object destruction sometime in the future...
>
> I agree, but like I said, I've been told that this (implicit closing
> of files) is the correct style by more merited Python developers, so
> that made me think I was probably wrong ..

Then tell those "more merited Python developers" that they're wrong, and  
that the right way to ensure a file is closed when you're done with it is  
to use a `with` statement (or a try/finally block in old Python releases)

-- 
Gabriel Genellina




More information about the Python-list mailing list