472,344 Members | 1,371 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,344 software developers and data experts.

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 1368
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Michael Sparks | last post by:
Anyway... At Europython Guido discussed with everyone the outstanding issue with decorators and there was a clear majority in favour of having...
4
by: Doug Holton | last post by:
First let me say please see the wiki page about python decorators if you haven't already: http://www.python.org/cgi-bin/moinmoin/PythonDecorators ...
8
by: Michele Simionato | last post by:
Decorators can generate endless debate about syntax, but can also be put to better use ;) Actually I was waiting for decorators to play a few...
12
by: Colin J. Williams | last post by:
Christopher T. King suggested that "we're trying to kill too many birds with one stone". ...
65
by: Miklós | last post by:
Ok, seems like we have @decorators. It's a nice tribute to Intercal. But I'd prefer #decorators. We have metaclasses for pleasure brain-melting....
37
by: Bengt Richter | last post by:
ISTM that @limited_expression_producing_function @another def func(): pass is syntactic sugar for creating a hidden list of functions. (Using...
2
by: Guido van Rossum | last post by:
Robert and Python-dev, I've read the J2 proposal up and down several times, pondered all the issues, and slept on it for a night, and I still...
2
by: Andrew West | last post by:
Probably a bit of weird question. I realise decorators shouldn't be executed until the function they are defined with are called, but is there...
0
by: Scott SA | last post by:
Hi, I've been trying to wrap my head around decorators and in my trails found avery recent article on Linux Mag's website. I didn't see a post...
0
better678
by: better678 | last post by:
Question: Discuss your understanding of the Java platform. Is the statement "Java is interpreted" correct? Answer: Java is an object-oriented...
0
by: teenabhardwaj | last post by:
How would one discover a valid source for learning news, comfort, and help for engineering designs? Covering through piles of books takes a lot of...
0
jalbright99669
by: jalbright99669 | last post by:
Am having a bit of a time with URL Rewrite. I need to incorporate http to https redirect with a reverse proxy. I have the URL Rewrite rules made...
0
by: antdb | last post by:
Ⅰ. Advantage of AntDB: hyper-convergence + streaming processing engine In the overall architecture, a new "hyper-convergence" concept was...
0
by: Matthew3360 | last post by:
Hi there. I have been struggling to find out how to use a variable as my location in my header redirect function. Here is my code. ...
0
by: Arjunsri | last post by:
I have a Redshift database that I need to use as an import data source. I have configured the DSN connection using the server, port, database, and...
0
hi
by: WisdomUfot | last post by:
It's an interesting question you've got about how Gmail hides the HTTP referrer when a link in an email is clicked. While I don't have the specific...
0
by: Matthew3360 | last post by:
Hi, I have been trying to connect to a local host using php curl. But I am finding it hard to do this. I am doing the curl get request from my web...
0
Oralloy
by: Oralloy | last post by:
Hello Folks, I am trying to hook up a CPU which I designed using SystemC to I/O pins on an FPGA. My problem (spelled failure) is with the...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.