Settings

Theme

Introduction to Python Decorators

artima.com

79 points by xlm 15 years ago · 14 comments

Reader

molbioguy 15 years ago

An excellent tutorial/answer about python decorators was posted on stack overflow:

http://stackoverflow.com/questions/739654/understanding-pyth...

  • vessenes 15 years ago

    I would replace "excellent" with "extremely thorough."

    I would recommend the posted link for a first introduction -- it's short, explains decorators as clearly but more approachably, and talks about their application to classes, something the SO answer misses.

  • sid6376 15 years ago

    Thanks for the link. As a python newbie, the way that helped me best was going through the stack overflow link and then the posted article. From what I understand its also important to understand when the decorator function is executed. Before or after the function it decorates.(I understand its also possible, that it executes both before and after). Is this understanding correct?

  • xlmOP 15 years ago

    Agreed, I read that one as well. What struck me was the link the author drew to LISP macros.

Goladus 15 years ago

The only thing I'd like to see more of with respect to decorators is discussion of real benefits. The concept is fairly simple it's the technique and resulting code organization that is interesting. Much like Lisp macros, it's important to know when not to use them and none of the tutorials I've seen really address that.

  • tripzilch 15 years ago

    a good example, when working with the Jinja2 templating language, when you want to declare your own modifier, a function that's called within the templating language like {bit of text|awesomify}, you'd just need to write a function with a decorator on it, like

        @jinja2_modifier
        def awesomify(s):
            return "%s is awesome" % s
    
    (except it wasn't "jinja2_modifier", look it up in the docs)
cgranade 15 years ago

The only thing that I might ever want from functional and symbolic programming paradigms that I can't imagine how to emulate with decorators would be the ability to transform the actual compiled function object in some way. Python decorators can add lots of new code, sure, but they don't get access to the syntax tree for the function object to be able to break it apart and transform it.

Mind you, I truly love Python decorators, but I just don't think the comparison to languages like LISP quite holds up. Maybe I'm just inexperienced, though...

  • daeken 15 years ago

    I'm going to hell for this, but here goes. A couple years back I wrote a library called Transformana ( https://github.com/daeken/Transformana ) which allows you to manipulate ASTs via 'macros' in decorators. I apologize from the bottom of my heart to anyone who ends up maintaining code that utilizes this, but have fun.

    https://github.com/daeken/Transformana/blob/master/test.py shows off various features if you're interested.

    Edit: Here's the output from running the test, in case you want to see the AST format.

        Hello world!
        Yep, working.
        Don't know.
        ['function',
         None,
         'test2',
         (),
         (),
         0,
         None,
         ['stmt', [['printnl', [['const', 'Hello from test2']], None]]]]
        Hello from test2
        ['function', None, 'test3', (), (), 0, None, ['stmt', [['printnl', [['const', 'This should never be callable']], None]]]]
    • softbuilder 15 years ago

      Fellow hell-bound soul here. I didn't go nearly as far as you did. I decided it would be fun to have guards in Python and decorators were just the tool. I sketched this out as part of a talk at Open Source Bridge a few years back:

      https://github.com/built/Jane-Kelly/blob/master/guard.py

      It'll let you redefine functions with different guard parameters. (@when("x=5"), @when("x>20"), etc.) Like you, I would also shudder to see this in production. But it was fun and shows off some of what's possible.

famousactress 15 years ago

Just me, or do others think any tutorial about python decorators ought to introduce and encourage the use of functools.wraps() ?

  • peregrine 15 years ago

    That would be nice! I poked around djangos source to see how they created decorators that take parameters but never thought how that worked was very intuitive.

    • IgorPartola 15 years ago

      It's easy:

        def mydec(func, decor_param_a, decor_param_b):
            print 'decor_param_a = %s' % decor_param_a
            def wrapper(*args, **kwargs):
                print 'about to run function'
                res = func(*args, **kwargs)
                print 'done running function'
                return res
      
            return wrapper
      
      Now just do:

        @mydecor('foo', 'bar')
        def baz():
            pass
      • stock_toaster 15 years ago

        It is a bit more work if you want to create a decorator that can:

        * decorate a class

        * decorate a method

        * have arguments

        * have no arguments

        extremely contrived example:

        http://pastebin.com/GBuYL2af

        Note that the above only works for 'new style' classes (otherwise the type signature is not 'type').

        edit: dumped code in a pastebin. it got kind of long.

ccarpenterg 15 years ago

A library of Python decorators: http://wiki.python.org/moin/PythonDecoratorLibrary

Keyboard Shortcuts

j
Next item
k
Previous item
o / Enter
Open selected item
?
Show this help
Esc
Close modal / clear selection