Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Decorators and how they relate to Python - A little insight please!

Reply
Thread Tools

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

 
 
Jerry
Guest
Posts: n/a
 
      10-19-2006
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

 
Reply With Quote
 
 
 
 
Gabriel Genellina
Guest
Posts: n/a
 
      10-19-2006
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

 
Reply With Quote
 
 
 
 
johnzenger@gmail.com
Guest
Posts: n/a
 
      10-19-2006
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


 
Reply With Quote
 
jmcantrell@gmail.com
Guest
Posts: n/a
 
      10-20-2006
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" <(E-Mail Removed)> 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


 
Reply With Quote
 
Fredrik Lundh
Guest
Posts: n/a
 
      10-20-2006
http://www.velocityreviews.com/forums/(E-Mail Removed) 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>

 
Reply With Quote
 
Fredrik Lundh
Guest
Posts: n/a
 
      10-20-2006
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>

 
Reply With Quote
 
Gabriel Genellina
Guest
Posts: n/a
 
      10-20-2006
At Friday 20/10/2006 02:38, (E-Mail Removed) 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
 
Reply With Quote
 
Jerry
Guest
Posts: n/a
 
      10-20-2006
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

 
Reply With Quote
 
Fuzzyman
Guest
Posts: n/a
 
      10-21-2006

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




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


> if you want more examples, see the cookbook
>
> http://www.google.com/search?q=+site...rator+cookbook
>
> </F>


 
Reply With Quote
 
Bruno Desthuilliers
Guest
Posts: n/a
 
      10-21-2006
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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: How include a large array? Edward A. Falk C Programming 1 04-04-2013 08:07 PM
1 little 2 little 3 little Kennedys dale Digital Photography 0 03-23-2008 01:03 PM
The physics of protons as they relate to noise and sensor size. ½ Confused Digital Photography 5 02-21-2006 02:22 PM
PEP 318 decorators are not Decorators Arien Malec Python 11 08-16-2004 06:38 PM
they turn, they power, they make nice pics Keith and Jenn Z. Digital Photography 0 09-21-2003 04:16 AM



Advertisments