Live data from Hacker News

Introduction to Python Decorators

artima.com

1–10 of 15 posts

Re: Introduction to Python Decorators

#3

An excellent tutorial/answer about python decorators was posted on stack overflow: http://stackoverflow.com/questions/739654/understanding-pyth...

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

Re: Introduction to Python Decorators

#4

An excellent tutorial/answer about python decorators was posted on stack overflow: http://stackoverflow.com/questions/739654/understanding-pyth...

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.

Re: Introduction to Python Decorators

#6

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

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.

Re: Introduction to Python Decorators

#8

An excellent tutorial/answer about python decorators was posted on stack overflow: http://stackoverflow.com/questions/739654/understanding-pyth...

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?

Re: Introduction to Python Decorators

#9
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...

Re: Introduction to Python Decorators

#10
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.
Post reply on HN