By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
444,100 Members | 2,495 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 444,100 IT Pros & Developers. It's quick & easy.

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

P: n/a
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
Share this Question
Share on Google+
10 Replies


P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a

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

P: n/a
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 discussion thread is closed

Replies have been disabled for this discussion.