What are decorators?

G

gohaku

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
 
P

Peter Hansen

gohaku said:
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
 
C

Colin J. Williams

gohaku said:
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
>
> 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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top