Decorators

Terry Reedy tjreedy at udel.edu
Sat Nov 15 15:28:41 EST 2014


On 11/15/2014 8:24 AM, Marko Rauhamaa wrote:

> I'd take top-posting if I were enlightened about how decorators could be
> valuable.

Here is part of the original rationale.

@deco(arg)
def f: suite

is (for this discussion) equivalent to

def f: suite
f = deco(arg)(f)

The latter requires writing 'f' 3 times instead of 1.  Now suppose to 
meet external requirements, such as interfacing to Objective C, you had 
to wrap dozens of functions, each with long names, up to 30 chars, with 
multiple components.  You would then become enlightened.

Once the idea of making function wrapping easier emerged, many other 
applications emerged.  Various stdlib modules define decorators.

If you do not need to wrap functions, easy wrapping might seem useless 
to you.  That's fine. You know they are there should you ever have need.

-- 
Terry Jan Reedy




More information about the Python-list mailing list