Kirk  Sluder <[EMAIL PROTECTED]> writes:
> Another common macro use is the "WITH-STREAM" family which opens a 
> stream for IO and closes it at the end.  
> (with-open-file (file-handle "filename" :direction :output)
>    (format file-handle "Hello world.~%")
> ) 
> 
> The pythonic way to do this would be to create a class that 
> implements file-like behaviors:
> 
> output = fileLike.open()
> output.write("Hello world\n")
> output.close()

Actually the Python example can lose (e.g. leak a file descriptor
temporarily) if output.write raises an exception (prevents
output.close from running).  For this reason Python recently
introduced the "with" statement:

    with output as fileLike.open():
       output.write("Hello world\n")

Here the file gets closed automatically (by running an exit method in
the fileLike class) when the "with" block exits, whether normally or
through an exception.
-- 
http://mail.python.org/mailman/listinfo/python-list

Reply via email to