What are decorators?

Discussion in 'Python' started by gohaku, Aug 9, 2004.

  1. gohaku

    gohaku Guest

    Hi everyone,
    The discussion on Python Decorators and "@" has piqued my interest on
    this
    "feature?" in programming languages such as Python and Java.
    can anybody what is the point in using Decorators?

    The examples I have seen written in Python of this "Not Yet
    Implemented" feature
    are confusing to say the least and perplexes me as to its usefulness.

    Thanks in advance.
    -gohaku
     
    gohaku, Aug 9, 2004
    #1
    1. Advertising

  2. gohaku

    Peter Hansen Guest

    gohaku wrote:

    > The discussion on Python Decorators and "@" has piqued my interest on this
    > "feature?" in programming languages such as Python and Java.
    > can anybody what is the point in using Decorators?


    In Python, at least, they seem to be syntactic sugar for the following,
    as the PEP clearly shows:

    def func():
    pass

    func = decorator(func)


    That is equivalent to this with the new proposed syntax (and this is
    no doubt out of date because I wrote it more than two minutes ago):

    @decorator
    def func():
    pass


    In other words, they are simply a function that takes a function
    and does something to or with it, returning a new function, or
    perhaps the old one, when it's done.

    The original use cases seem to have been staticmethod and classmethod.
    Python doesn't have special syntax for defining these, as for
    example Java does, so the idiom shown first above was developed,
    along with a staticmethod() or classmethod() "decorator" function
    which would modify the original non-static method so that it was
    now a static method.

    The folks using this decided they didn't like the fact that the
    modification came *after* the function definition, since it could
    be hard to notice it, and probably they didn't really like the
    feel of the whole thing, since it's sort of hackish and inelegant.

    If you don't need staticmethod (and the answer to the question
    "do I need staticmethod?" is "no"), then you don't really need
    decorators. :)

    -Peter
     
    Peter Hansen, Aug 9, 2004
    #2
    1. Advertising

  3. gohaku wrote:
    > Hi everyone,
    > The discussion on Python Decorators and "@" has piqued my interest on this
    > "feature?" in programming languages such as Python and Java.
    > can anybody what is the point in using Decorators?
    >

    The term decorator is a bit misleading. It does not decorate or adorn a
    function, class or method declaration, the declaration is transformed.

    There are cases where one might wish to change the behaviour of a
    function to, for example ensure that the arguments being passed in
    are of a certain class or that the object returned has given type.


    > The examples I have seen written in Python of this "Not Yet Implemented"
    > feature
    > are confusing to say the least and perplexes me as to its usefulness.
    >
    > Thanks in advance.
    > -gohaku
    >

    In a posting yerterday, Dan bishop wrote:

    >
    > If I understand correctly, they'd be useful for anything where you'd
    > now use the syntax
    >
    > function = decorator(function)
    >
    > In addition to @staticmethod, you could have decorators for
    >
    > (1) Memoization. Makes repeated function evaluation more efficient
    > without having to rewrite the function.
    >
    > class memoize(object):
    > def __init__(self, func):
    > self.__func = func
    > self.__results = {}
    > def __call__(self, *args):
    > if args not in self.__results:
    > self.__results[args] = self.__func(*args)
    > return self.__results[args]
    >
    > def fibonacci(n):
    > @memoize
    > if n in (0, 1):
    > return n
    > return fibonacci(n - 1) + fibonacci(n - 2)
    >
    > (2) Debugging uses, like:
    >
    > class printreturns(object):
    > "Behaves like f but prints its return values."
    > def __init__(self, f):
    > self.__f = f
    > def __call__(self, *args):
    > result = self.__f(*args)
    > if debug:
    > print 'f%r = %r' % (args, result)
    > return
    > def somefunc(x, y):
    > @printreturns
    > ...


    I found it helpful, I hope that you do.

    Colin W.
     
    Colin J. Williams, Aug 9, 2004
    #3
    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. Michael Strorm
    Replies:
    12
    Views:
    1,790
    Michael Strorm
    Apr 20, 2005
  2. Michael Sparks

    Using metaclasses to play with decorators.

    Michael Sparks, Jun 15, 2004, in forum: Python
    Replies:
    4
    Views:
    339
    Michele Simionato
    Jun 18, 2004
  3. Michael Sparks
    Replies:
    6
    Views:
    355
    David MacQuigg
    Jun 18, 2004
  4. Robert Brewer
    Replies:
    9
    Views:
    316
    David MacQuigg
    Jun 26, 2004
  5. Arien Malec

    PEP 318 decorators are not Decorators

    Arien Malec, Aug 13, 2004, in forum: Python
    Replies:
    11
    Views:
    572
    Arien Malec
    Aug 16, 2004
Loading...

Share This Page