merits of Lisp vs Python

Paul Rubin http
Sun Dec 10 01:15:04 EST 2006


Kirk  Sluder <kirk at nospam.jobsluder.net> 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.



More information about the Python-list mailing list