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

J

Jerry

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
 
G

Gabriel Genellina

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/python/documentation.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
 
J

johnzenger

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.
 
J

jmcantrell

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
 
F

Fredrik Lundh

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>
 
G

Gabriel Genellina

At said:
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
 
J

Jerry

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).
 
F

Fuzzyman

Fredrik said:

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/
 
B

Bruno Desthuilliers

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/developerworks/library/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
 
S

sjdevnull

Jerry said:
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):
...
 

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

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,904
Latest member
HealthyVisionsCBDPrice

Latest Threads

Top