does python have useless destructors?

Michael Geary Mike at DeleteThis.Geary.com
Thu Jun 10 09:47:02 CEST 2004


Hannu Kankaanp??
> The correct idiom is:
>
> myfile = file("myfilepath", "w")
> try:
>     myfile.write(reallybigbuffer)
> finally:
>     myfile.close()
>
> I'd very much prefer something simpler that leaves no room
> for errors, e.g.
>
> with myfile = file("myfilepath", "w"):
>     myfile.write(reallybigbuffer)
>
> (this, and more generic solutions with code blocks have been
> suggested here many times)

That's just like the way you would code it in Ruby:

File.open( 'myfilepath', 'w' ) do |myfile|
    myfile.write( reallybigbuffer )
end

-Mike





More information about the Python-list mailing list