469,887 Members | 1,334 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,887 developers. It's quick & easy.

What are decorators?

Hi everyone,
The discussion on Python Decorators and "@" has piqued my interest on
this
"feature?" in programming languages such as Python and Java.
can anybody what is the point in using Decorators?

The examples I have seen written in Python of this "Not Yet
Implemented" feature
are confusing to say the least and perplexes me as to its usefulness.

Thanks in advance.
-gohaku

Jul 18 '05 #1
2 1297
gohaku wrote:
The discussion on Python Decorators and "@" has piqued my interest on this
"feature?" in programming languages such as Python and Java.
can anybody what is the point in using Decorators?


In Python, at least, they seem to be syntactic sugar for the following,
as the PEP clearly shows:

def func():
pass

func = decorator(func)
That is equivalent to this with the new proposed syntax (and this is
no doubt out of date because I wrote it more than two minutes ago):

@decorator
def func():
pass
In other words, they are simply a function that takes a function
and does something to or with it, returning a new function, or
perhaps the old one, when it's done.

The original use cases seem to have been staticmethod and classmethod.
Python doesn't have special syntax for defining these, as for
example Java does, so the idiom shown first above was developed,
along with a staticmethod() or classmethod() "decorator" function
which would modify the original non-static method so that it was
now a static method.

The folks using this decided they didn't like the fact that the
modification came *after* the function definition, since it could
be hard to notice it, and probably they didn't really like the
feel of the whole thing, since it's sort of hackish and inelegant.

If you don't need staticmethod (and the answer to the question
"do I need staticmethod?" is "no"), then you don't really need
decorators. :-)

-Peter
Jul 18 '05 #2


gohaku wrote:
Hi everyone,
The discussion on Python Decorators and "@" has piqued my interest on this
"feature?" in programming languages such as Python and Java.
can anybody what is the point in using Decorators?
The term decorator is a bit misleading. It does not decorate or adorn a
function, class or method declaration, the declaration is transformed.

There are cases where one might wish to change the behaviour of a
function to, for example ensure that the arguments being passed in
are of a certain class or that the object returned has given type.

The examples I have seen written in Python of this "Not Yet Implemented"
feature
are confusing to say the least and perplexes me as to its usefulness.

Thanks in advance.
-gohaku
In a posting yerterday, Dan bishop wrote:

If I understand correctly, they'd be useful for anything where you'd
now use the syntax

function = decorator(function)

In addition to @staticmethod, you could have decorators for

(1) Memoization. Makes repeated function evaluation more efficient
without having to rewrite the function.

class memoize(object):
def __init__(self, func):
self.__func = func
self.__results = {}
def __call__(self, *args):
if args not in self.__results:
self.__results[args] = self.__func(*args)
return self.__results[args]

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

(2) Debugging uses, like:

class printreturns(object):
"Behaves like f but prints its return values."
def __init__(self, f):
self.__f = f
def __call__(self, *args):
result = self.__f(*args)
if debug:
print 'f%r = %r' % (args, result)
return
def somefunc(x, y):
@printreturns
...


I found it helpful, I hope that you do.

Colin W.

Jul 18 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

4 posts views Thread by Michael Sparks | last post: by
8 posts views Thread by Michele Simionato | last post: by
12 posts views Thread by Colin J. Williams | last post: by
65 posts views Thread by Miklós | last post: by
37 posts views Thread by Bengt Richter | last post: by
2 posts views Thread by Guido van Rossum | last post: by
2 posts views Thread by Andrew West | last post: by
reply views Thread by Scott SA | last post: by
1 post views Thread by Waqarahmed | last post: by
reply views Thread by Salome Sato | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.