What are decorated functions?

Discussion in 'Python' started by Wolfgang Draxinger, Aug 22, 2006.

  1. I'm just reading the python language reference and came around
    the term "decorated functions", but I have no idea, for what
    they could be used.

    Any reply gracefully accepted.

    Wolfgang Draxinger
    --
    E-Mail address works, Jabber: , ICQ: 134682867
    GPG key FP: 2FC8 319E C7D7 1ADC 0408 65C6 05F5 A645 1FD3 BD3E
    Wolfgang Draxinger, Aug 22, 2006
    #1
    1. Advertising

  2. PEP 318 provides some great examples:
    http://www.python.org/dev/peps/pep-0318/

    For more information no the decorator pattern in general:
    http://en.wikipedia.org/wiki/Decorator_pattern

    How one actually makes use of decorators beyond the above is an
    exercise of imagination.

    Wolfgang Draxinger wrote:
    > I'm just reading the python language reference and came around
    > the term "decorated functions", but I have no idea, for what
    > they could be used.
    >
    > Any reply gracefully accepted.
    >
    > Wolfgang Draxinger
    > --
    > E-Mail address works, Jabber: , ICQ: 134682867
    > GPG key FP: 2FC8 319E C7D7 1ADC 0408 65C6 05F5 A645 1FD3 BD3E
    Daniel O'Brien, Aug 23, 2006
    #2
    1. Advertising

  3. Daniel O'Brien wrote:

    > How one actually makes use of decorators beyond the above is an
    > exercise of imagination.


    Now it rings a bell, thx.

    Wolfgang Draxinger
    --
    E-Mail address works, Jabber: , ICQ: 134682867
    GPG key FP: 2FC8 319E C7D7 1ADC 0408 65C6 05F5 A645 1FD3 BD3E
    Wolfgang Draxinger, Aug 23, 2006
    #3
  4. At Tuesday 22/8/2006 17:19, Wolfgang Draxinger wrote:

    >I'm just reading the python language reference and came around
    >the term "decorated functions", but I have no idea, for what
    >they could be used.


    A decorator takes a function/method/callable, "decorates" (modifies)
    it in a certain way, and returns it.
    @classmethod and @staticmethod are examples: they take a (normal)
    method, and transform it into another thing.
    You can write your own decorators. An example similar to one
    discussed on this list a few days ago: suppose you have a pure
    function taking a long time to compute; you could save the results in
    a dictionary using the arguments as key, and retrieve them later when
    a call is made exactly with the same arguments. This is a known
    pattern ("memoize").

    def memoized(function):
    function.cache={}
    def f(*args):
    try: return function.cache[args]
    except KeyError:
    result = function.cache[args] = function(*args)
    return result
    return f

    @memoized
    def fibo(n):
    time.sleep(2) # a sloooooow function
    if n<2: return 1
    return fibo(n-1)+fibo(n-2)

    The original function is fibo() - it works OK but assume it's slow.
    So you "decorate" it with @memoized to improve performance.
    (This is just an example, of course - I'm not saying memoizing is the
    right thing to do here, nor this is the best way to do it... just to
    demonstrate what a decorator could be used for)



    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, Aug 23, 2006
    #4
  5. Gabriel Genellina wrote:
    > At Tuesday 22/8/2006 17:19, Wolfgang Draxinger wrote:
    >> I'm just reading the python language reference and came around
    >> the term "decorated functions", but I have no idea, for what
    >> they could be used.

    >
    > A decorator takes a function/method/callable, "decorates" (modifies)
    > it in a certain way, and returns it.


    It's important to note here that decorators, using the @decorate syntax[1],
    *can only decorate methods and functions*. That is, they must be followed
    by either another @decorator or a "def" statement. Anything else will
    result in a SyntaxError. For example:

    >>> def test(callable): return callable

    ....
    >>> @test

    .... def foo(): pass
    ....
    >>> @test

    .... @test
    .... @test
    .... def foo(): pass
    ....
    >>> @test

    .... class bar:
    File "<stdin>", line 2
    class bar:
    ^
    SyntaxError: invalid syntax
    >>> @test

    .... file
    File "<stdin>", line 2
    file
    ^
    SyntaxError: invalid syntax
    >>>


    So you can't decorate classes (the original PEP proposed it, but it was
    dropped - see the discussion about this on python-dev[2]). You *can*
    decorate a class' __init__ method, but that's not quite the same as eg.
    implementing @singleton (mind you, we already have a bazillion ways to
    implement the singleton pattern, so I don't think we're poorer for this
    limitation <wink>)


    Richard

    [1] of course, you can "anything = decorate(anything)"
    [2] http://mail.python.org/pipermail/python-dev/2005-March/052369.html
    Richard Jones, Aug 23, 2006
    #5
    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. Replies:
    1
    Views:
    1,839
    Roedy Green
    Sep 17, 2005
  2. Zaza
    Replies:
    0
    Views:
    611
  3. jelsas
    Replies:
    2
    Views:
    351
    jelsas
    Dec 10, 2008
  4. Steven D'Aprano
    Replies:
    2
    Views:
    284
    Michele Simionato
    Feb 15, 2009
  5. Steven D'Aprano

    How to peek inside a decorated function

    Steven D'Aprano, Feb 15, 2009, in forum: Python
    Replies:
    6
    Views:
    334
    Peter Otten
    Feb 15, 2009
Loading...

Share This Page