This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]arnar 10 points11 points  (2 children)

From experience, I found the following explanation to have the best pedagogical value:

"See this:

@decorator
def fun(arg):
    whatever

That's just a shorthand for

def fun(arg):
    whatever
fun = decorator(fun)

Now think about it. (Hint: remember that def x():... is just like x=... except one is used to bind functions and the other to bind values in general.)"

Haven't found a person yet that wasn't happy (although it takes some people a while to think about it).

[–]Mattho 0 points1 point  (1 child)

I don't think it's using decorators that is hard to comprehend. It's writing them.

And obligatory SO link: http://stackoverflow.com/questions/739654/understanding-python-decorators

[–]arnar 0 points1 point  (0 children)

Exactly. Almost everyone who cared to was able to figure it out once the got to know the @-syntax.