472,333 Members | 1,107 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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

Decorators and how they relate to Python - A little insight please!

I have just started to do some semi-serious programming (not one-off
specialized scripts) and am loving Python. I just seem to get most of
the concepts, the structure, and the classes (something I struggled
with before in other languages). I've seen many concepts on the web
(i.e. stacks, queues, linked lists) and can usually understand them
pretty well (event if I don't always know when to use them). Now I've
come accross decorators and even though I've read the PEP and a little
in the documentation, I just don't get what they are or what problem
they are trying to solve. Can anyone please point me to a general
discussion of decorators (preferrably explained with Python)?

Thanks,
Jerry

Oct 19 '06 #1
10 1291
At Thursday 19/10/2006 15:04, Jerry wrote:
>Now I've
come accross decorators and even though I've read the PEP and a little
in the documentation, I just don't get what they are or what problem
they are trying to solve. Can anyone please point me to a general
discussion of decorators (preferrably explained with Python)?
These links may be useful:

<URL: http://www.phyast.pitt.edu/~micheles...mentation.html >
<URL: http://soiland.no/software/decorator >
<URL: http://wiki.python.org/moin/PythonDecoratorLibrary >
--
Gabriel Genellina
Softlab SRL

__________________________________________________
Preguntá. Respondé. Descubrí.
Todo lo que querías saber, y lo que ni imaginabas,
está en Yahoo! Respuestas (Beta).
¡Probalo ya!
http://www.yahoo.com.ar/respuestas

Oct 19 '06 #2
When you want to repeatedly apply a certain ALGORITHM to arbitrary sets
of DATA, you write a FUNCTION.

When you want to add a certain BEHAVIOR to arbitrary sets of FUNCTIONS,
you write a DECORATOR.

An excellent use of decorators is in Django, the web programming
framework. Django keeps track of usernames and passwords for you.
Occasionally, there are some things on your web site that you only want
to only let people who are logged in do (like, say, leave a comment on
your blog). Instead of making you begin every such function with "if
user_is_logged in:" or some similar abomination, Django lets you just
put a @require_login decorator before that function. Pretty spiffy.
Jerry wrote:
I have just started to do some semi-serious programming (not one-off
specialized scripts) and am loving Python. I just seem to get most of
the concepts, the structure, and the classes (something I struggled
with before in other languages). I've seen many concepts on the web
(i.e. stacks, queues, linked lists) and can usually understand them
pretty well (event if I don't always know when to use them). Now I've
come accross decorators and even though I've read the PEP and a little
in the documentation, I just don't get what they are or what problem
they are trying to solve. Can anyone please point me to a general
discussion of decorators (preferrably explained with Python)?

Thanks,
Jerry
Oct 19 '06 #3
it's handy for doing things like validation of parameter and return
types. Like...

@accepts(int,int)
@returns(int)
def add(a,b):
return a+b

On Oct 19, 2:04 pm, "Jerry" <jwe...@gmail.comwrote:
I have just started to do some semi-serious programming (not one-off
specialized scripts) and am loving Python. I just seem to get most of
the concepts, the structure, and the classes (something I struggled
with before in other languages). I've seen many concepts on the web
(i.e. stacks, queues, linked lists) and can usually understand them
pretty well (event if I don't always know when to use them). Now I've
come accross decorators and even though I've read the PEP and a little
in the documentation, I just don't get what they are or what problem
they are trying to solve. Can anyone please point me to a general
discussion of decorators (preferrably explained with Python)?

Thanks,
Jerry
Oct 20 '06 #4
jm********@gmail.com wrote:
it's handy for doing things like validation of parameter and return
types. Like...

@accepts(int,int)
@returns(int)
def add(a,b):
return a+b
using Python decorators to turn Python into something that's not Python
doesn't seem very handy to me, though.

</F>

Oct 20 '06 #5
Jerry wrote:
even though I've read the PEP
even the examples section?

http://www.python.org/dev/peps/pep-0318/#examples

if you want more examples, see the cookbook

http://www.google.com/search?q=+site...rator+cookbook

</F>

Oct 20 '06 #6
At Friday 20/10/2006 02:38, jm********@gmail.com wrote:
>it's handy for doing things like validation of parameter and return
types. Like...

@accepts(int,int)
@returns(int)
def add(a,b):
return a+b
So, it's handy for converting Python into another language :)
--
Gabriel Genellina
Softlab SRL

__________________________________________________
Correo Yahoo!
Espacio para todos tus mensajes, antivirus y antispam ¡gratis!
¡Abrí tu cuenta ya! - http://correo.yahoo.com.ar
Oct 20 '06 #7
Thanks to everyone that resonded. I will have to spend some time
reading the information that you've provided.

To Fredrik, unfortunately yes. I saw the examples, but couldn't get my
head wrapped around their purpose. Perhaps it's due to the fact that
my only experience with programming is PHP, Perl and Python and to my
knowledge only Python supports decorators (though it could be that I
just didn't encounter them until I came across Python).

--
Jerry

Oct 20 '06 #8
Jerry a écrit :
Thanks to everyone that resonded. I will have to spend some time
reading the information that you've provided.

To Fredrik, unfortunately yes. I saw the examples, but couldn't get my
head wrapped around their purpose. Perhaps it's due to the fact that
my only experience with programming is PHP, Perl and Python and to my
knowledge only Python supports decorators (though it could be that I
just didn't encounter them until I came across Python).
Python's "decorators" are just a possible use of "higher order
functions" (aka HOFs) - functions that takes functions as arguments
and/or return functions. HOFs are the base of functional programming
(Haskell, ML, Lisp, etc), and rely on functions being first class
citizens (which is the case in Python where functions are objects).
While Python is not truly a functional language, it has a good enough
support for functional programing, and happily mixes functional
programming and OO concepts.

If you have no prior exposure to a functional language, it's not
surprinsing you have some hard time understanding decorators and their
possible uses. I suggest you read David Mertz's articles on FP in Python:
http://www-128.ibm.com/developerwork...ry/l-prog.html

While these articles predate the @decorator syntax (and FWIW makes
absolutely no mention of decorators), it should help you grasping basic
Python's functional idioms.

HTH
Oct 21 '06 #9

Fredrik Lundh wrote:
Jerry wrote:
even though I've read the PEP

even the examples section?

http://www.python.org/dev/peps/pep-0318/#examples
The second example of which shows :

Define a class with a singleton instance. Note that once the class
disappears enterprising programmers would have to be more creative to
create more instances. (From Shane Hathaway on python-dev.)

def singleton(cls):
instances = {}
def getinstance():
if cls not in instances:
instances[cls] = cls()
return instances[cls]
return getinstance

@singleton
class MyClass:
...
:-o

Fuzzyman
http://www.voidspace.org.uk/

if you want more examples, see the cookbook

http://www.google.com/search?q=+site...rator+cookbook

</F>
Oct 21 '06 #10
Jerry wrote:
Thanks to everyone that resonded. I will have to spend some time
reading the information that you've provided.

To Fredrik, unfortunately yes. I saw the examples, but couldn't get my
head wrapped around their purpose.
You're probably looking at the newfangled @decorator syntax.

Things were a lot clearer when decorators were called explicitly:

class foo(object):
def bar(self):
...
bar = classmethod(bar)

That makes it a lot more obvious that classmethod is simply a
higher-order function (that is, it takes a function as an argument and
returns a function).

It is equivalent to the newer:

class foo(object):
@classmethod
def bar(self):
...

Oct 21 '06 #11

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

11
by: Arien Malec | last post by:
I've been following the decorator debate with some interest, and it's taken me a reasonably long time to understand what is meant by a decorator....
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...
0
by: Anthony Baxter | last post by:
To go along with the 2.4a3 release, here's an updated version of the decorator PEP. It describes the state of decorators as they are in 2.4a3. ...
13
by: km | last post by:
Hi all, was going thru the new features introduced into python2.4 version. i was stuck with 'decorators' - can someone explain me the need of such...
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
by: Kemmylinns12 | last post by:
Blockchain technology has emerged as a transformative force in the business world, offering unprecedented opportunities for innovation and...
0
by: CD Tom | last post by:
This happens in runtime 2013 and 2016. When a report is run and then closed a toolbar shows up and the only way to get it to go away is to right...
0
by: CD Tom | last post by:
This only shows up in access runtime. When a user select a report from my report menu when they close the report they get a menu I've called Add-ins...
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. ...
2
by: Matthew3360 | last post by:
Hi, I have a python app that i want to be able to get variables from a php page on my webserver. My python app is on my computer. How would I make it...
0
by: AndyPSV | last post by:
HOW CAN I CREATE AN AI with an .executable file that would suck all files in the folder and on my computerHOW CAN I CREATE AN AI with an .executable...

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.