@decorator syntax is sugar, but for what exactly? (decorator libraries).

Roy Smith roy at panix.com
Sun Aug 8 15:32:21 CEST 2004


Anthony Baxter <anthonybaxter at gmail.com> wrote:
> The "Python of 1.5.2 simplicitly" is long, long gone. I don't agree that
> newer Python's are somehow worse because new things have been
> added. A short list:
> 
>   new style classes
>   foo(*arg, **kwarg)
>   iterators
>   generators 
>   list comprehensions

Perhaps I'm just a luddite, but I don't actually use most of those 
features.  I have started playing around with iterators/generators, and 
find them very cool.

The single biggest improvement I see in the language since 1.5.2 is 
string methods!  After that, maybe augmented assignments (or whatever 
you call them; the ability to write "x += 1").

Most of the big improvements I've seen are in the library.  When did 
unitest get added?  I can't live without unittest.  I like the logging 
module, even if I think it's about twice as complicated as it should be.

A lot of my own personal growth in how I use the language is discovering 
modules which, while not new to the language, are new to me because I'd 
never noticed them before.

Speaking of libraries, Dan Bishop posted some interesting example of 
@memoize and @printreturns utility wrappers.  This leads me to think 
that a good way to leverage the idea of decorators would be a module of 
common utility functions which could be used as decorators by anybody.
I'll call the module martha (since it supplies things used for 
decorating).  Does the proposed mechanism support something like (to use 
one of Dan's exmaples, written with two different syntaxen):

import martha

@martha.memoize
def fibonacci(n):
   if n in (0, 1):
      return n
   return fibonacci(n - 1) + fibonacci(n - 2)

def fibonacci(n):
   @martha.memoize
   if n in (0, 1):
      return n
   return fibonacci(n - 1) + fibonacci(n - 2)

Some of these things might even be usefully re-written in C for improved 
performance.

I'm even wondering if somehow decorators could be used for i18n?  The 
obvious problem there, is print is a statement not a function, and you 
can't decorate statements (or can you???).



More information about the Python-list mailing list