Decorator 1.2 Python script

SPONSORED LINKS

    Specification

  • Version: 1.2
  • File size: 0 KB
  • File name: index_txt
  • Last update:
  • Platform: Windows / Linux / Mac OS / BSD / Solaris
  • Language: Python
  • Price:Other Free / Open So
  • Company: Carl Banks (View more)

Decorator 1.2 script description:




Publisher review:
Decorator script defines a "decorator" that, rather than decorating the function, calls it, passing it a file object that it had opened as the first argument. This script defines a "decorator" 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 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 decorators to get a with-open-file block anyways. The call_with_open_file decorator 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 like the one in this recipe could help.
Decorator 1.2 is a Python script for File Management scripts design by Carl Banks. It runs on following operating system: Windows / Linux / Mac OS / BSD / Solaris.
defines a

Operating system:
Windows / Linux / Mac OS / BSD / Solaris

Latest script and internet news

222

222

22

Posted on: 18 Jul 2023 22:27 by A. Brown

111

111

111

Posted on: 18 Jul 2023 22:24 by A. Brown

The permanently active Push system offered by the new Google Chrome 42

The permanently active Push system offered by the new Google Chrome 42

Hacked By !Sc-sT

Posted on: 17 Mar 2015 07:57 by A. Brown

SPREAD THE WORD

User Rating


Rating: 2.2 out of 5
Based on 13 ratings. 13 user reviews.

  • Currently 2.15 out of 5
  • 1
  • 2
  • 3
  • 4
  • 5