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

Discussion in 'Python' started by Jerry, Oct 19, 2006.

  1. Jerry

    Jerry Guest

    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
     
    Jerry, Oct 19, 2006
    #1
    1. Advertising

  2. Re: Decorators and how they relate to Python - A littleinsight please!

    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/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
     
    Gabriel Genellina, Oct 19, 2006
    #2
    1. Advertising

  3. Jerry

    Guest

    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, 2006
    #3
  4. Jerry

    Guest

    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" <> 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 20, 2006
    #4
  5. Re: Decorators and how they relate to Python - A little insightplease!

    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>
     
    Fredrik Lundh, Oct 20, 2006
    #5
  6. Fredrik Lundh, Oct 20, 2006
    #6
  7. Re: Decorators and how they relate to Python - A littleinsight please!

    At Friday 20/10/2006 02:38, 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
     
    Gabriel Genellina, Oct 20, 2006
    #7
  8. Jerry

    Jerry Guest

    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
     
    Jerry, Oct 20, 2006
    #8
  9. Jerry

    Fuzzyman Guest

    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:aspn.activestate.com decorator cookbook
    >
    > </F>
     
    Fuzzyman, Oct 21, 2006
    #9
  10. 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
     
    Bruno Desthuilliers, Oct 21, 2006
    #10
  11. Jerry

    Guest

    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, 2006
    #11
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. keithb
    Replies:
    1
    Views:
    2,783
    Teemu Keiski
    Apr 8, 2006
  2. Arien Malec

    PEP 318 decorators are not Decorators

    Arien Malec, Aug 13, 2004, in forum: Python
    Replies:
    11
    Views:
    587
    Arien Malec
    Aug 16, 2004
  3. Li Daobing
    Replies:
    1
    Views:
    411
    Tom Widmer
    Jun 21, 2006
  4. Ben Daniel
    Replies:
    4
    Views:
    2,820
    Ben Daniel
    Oct 14, 2007
  5. markla
    Replies:
    1
    Views:
    580
    Steven Cheng
    Oct 6, 2008
Loading...

Share This Page