Live data from Hacker News

Things which aren't magic – Flask and app.route

ains.co

1–10 of 35 posts

Re: Things which aren't magic – Flask and app.route

#4
post #3

That. Things _that_ aren't magic. On a more relevant note, thanks for sharing this. It's always nice to read an explanation from somebody patient enough to not skip over a bunch of steps in the middle and avoid losing newbs like me.

"That" and "which" are apparently interchangeable on the other side of the pond, and the author is from Imperial College London.

On a more relevant note, I agree, this is a great article. :) (I'd already known in theory how decorators work, but this was a very clear presentation, and I hadn't quite thought through the part about only needing a reference to the unmodified function, so it was useful to see that trick spelled out.)

Re: Things which aren't magic – Flask and app.route

#5
What's throwing me off -- and I'm sure it's something trivial I'm not seeing -- is how we add the functions to the routes dictionary. When is the decorator's code actually executed? Is that done at import time? My assumption is that its code is executed when its counterpart is called, though clearly that mustn't be the case.

Given my understanding from the article, there's a hole: how does serving route "/" know to call that function if (given my assumptions) the @app.route("/") decoration is not executed until the function it decorates is called?

Re: Things which aren't magic – Flask and app.route

#6

What's throwing me off -- and I'm sure it's something trivial I'm not seeing -- is how we add the functions to the routes dictionary. When is the decorator's code actually executed? Is that done at import time? My assumption is that its code is executed when its counterpart is called, though clearly that mustn't be the case. Given my understanding from the article, there's a hole: how does serving route "/" know to c…

Plain decorators work how you expect but generators are different. The decorator generator is called at import time, which is when the association between the route and the function is stored.

Re: Things which aren't magic – Flask and app.route

#7

What's throwing me off -- and I'm sure it's something trivial I'm not seeing -- is how we add the functions to the routes dictionary. When is the decorator's code actually executed? Is that done at import time? My assumption is that its code is executed when its counterpart is called, though clearly that mustn't be the case. Given my understanding from the article, there's a hole: how does serving route "/" know to c…

app.route("/") is just a function call which will be run by Python on module import, like any other function that you call in the top level of your module.

The return value of the function is a decorator, so then the @ syntax applies it to the succeeding function. In the process of applying the decorator, it adds the route to the dictionary. That's also on module import.

Re: Things which aren't magic – Flask and app.route

#8
post #6

What's throwing me off -- and I'm sure it's something trivial I'm not seeing -- is how we add the functions to the routes dictionary. When is the decorator's code actually executed? Is that done at import time? My assumption is that its code is executed when its counterpart is called, though clearly that mustn't be the case. Given my understanding from the article, there's a hole: how does serving route "/" know to c…

Plain decorators work how you expect but generators are different. The decorator generator is called at import time, which is when the association between the route and the function is stored.

How is the decorator mentioned in the article a decorator generator?

Edit:Sorry. I did not notice the term decorator generator in the article. Thanks for the explanation

Re: Things which aren't magic – Flask and app.route

#9
post #3

That. Things _that_ aren't magic. On a more relevant note, thanks for sharing this. It's always nice to read an explanation from somebody patient enough to not skip over a bunch of steps in the middle and avoid losing newbs like me.

> That. Things _that_ aren't magic.

"Then saith he unto them, Render therefore unto Caesar the things which are Caesar's; and unto God the things that are God's."

Clearly Flask is Caesar's ;-)

Post reply on HN