Of course, it makes sure to close the file upon the function's return (or nonreturn) ...Decorator script download" />

Decorator 1.2

Operating systemsOS : Windows / Linux / Mac OS / BSD / Solaris
Program licensingScript Licensing : Other Free / Open Source License - Python License
CreatedCreated : Jun 6, 2007
Size downloadDownloads : 1
Program licensing
Thank you for voting...

This script defines a "decorator" that, rather than ...

This script defines a "Decorator by Carl Banks" that, rather than decorating the function, calls it, passing it a file object that it had opened as the first argument.
Of course, it makes sure to close the file upon the function's return (or nonreturn) with a try. . . finally block. One can use this Decorator 1.2 to get the effect of a Lisp-like with_open_file block in Python.
In Common Lisp, there is a very convenient macro called with-open-file that opens a file, binds the file_object to a block-local variable, and then executes the block. The file is automatically closed when that block is exited.
Not having macros in Python, we generally do this with a try. . . finally statement, which works fine.
But let's try to (ab)use Decorator [decorator1.2.exe]s to get a with-open-file block anyways. The call_with_open_file Decorator - 0MB kind of gives us this effect: the decorated function acts as a code block. However, unlike try. . . finally, it has the limitations of nested functions, so unfortunately this recipe is not quite so useful as the Lisp macro.
Nevertheless, it might stil be useful in the occasional sitation where you have to use libraries that don't let you create and finalize objects yourself, but force you to use a callback. Having no way to use a try. . . finally, and needing to reference (but hopefully not rebind) local variables, a Decorator 1.2 like the one in this recipe could help.

Decorator 1.2 scripting tags: tryfinally, function decorator, file, file object, block, withopenfile, file tool. What is new in Decorator 1.2 software script? - Unable to find Decorator 1.2 news. What is improvements are expecting? Newly-made Decorator 1.3 will be downloaded from here. You may download directly. Please write the reviews of the Decorator. License limitations are unspecified.