Live data from Hacker News

A primer on Python decorators

thumbtack.com

1–10 of 45 posts

Re: A primer on Python decorators

#3
There is a small mistake in the post: decorators cannot be arbitrary expressions. Something like @Foo(spam).bar fails with an unhelpful SyntaxError, something that everyone who designs complex APIs will probably encounter at some point. This is a restriction which is in place because Guido didn't like arbitrary expressions as decorators.

Re: A primer on Python decorators

#4
post #3

There is a small mistake in the post: decorators cannot be arbitrary expressions. Something like @Foo(spam).bar fails with an unhelpful SyntaxError, something that everyone who designs complex APIs will probably encounter at some point. This is a restriction which is in place because Guido didn't like arbitrary expressions as decorators.

Oh, Guido. Thanks, I've updated the post.

Re: A primer on Python decorators

#6
post #2

Great primer, reading it gives a good sense for why the decorator syntax was made and the potential uses. Is this part of a wider series you are running?

Thanks! It is indeed the first in a series of Python posts that will be on the Thumbtack engineering blog.

Re: A primer on Python decorators

#7
What I really want to know is how a framework such as Flask uses a decorator for the route. How is the correct function picked for a particular route that is defined against the decorator? (Maybe I'm completely misunderstanding this...)

Re: A primer on Python decorators

#8
post #7

What I really want to know is how a framework such as Flask uses a decorator for the route. How is the correct function picked for a particular route that is defined against the decorator? (Maybe I'm completely misunderstanding this...)

The decorator can do anything. In flask the decorator syntax is just shorthand for adding the route to a registry which is looked up every request.

The source code is pitifully small: https://bitbucket.org/mitsuhiko/flask/src/4d82231621fc/flask...

Re: A primer on Python decorators

#9
post #7

What I really want to know is how a framework such as Flask uses a decorator for the route. How is the correct function picked for a particular route that is defined against the decorator? (Maybe I'm completely misunderstanding this...)

Calling a decorator can mutate some shared state.

Re: A primer on Python decorators

#10
post #7

What I really want to know is how a framework such as Flask uses a decorator for the route. How is the correct function picked for a particular route that is defined against the decorator? (Maybe I'm completely misunderstanding this...)

Here's the source of Flask's `route` decorator:

https://github.com/mitsuhiko/flask/blob/master/flask/app.py#...

When you call `route` with a URL pattern, it returns an inner function which is used as the decorator. That decorator just records your route and function in the Flask URL map, and returns your function unchanged.

So, Flask is arguably perpetuating a slight abuse of decorators, since it doesn't decorate or wrap your function at all, but merely saves a reference to it somewhere. But it's a fairly clean way to make up for the lack of code blocks or multi-statement anonymous functions in Python.

Post reply on HN