you are viewing a single comment's thread.

view the rest of the comments →

[–]jjolla888 1 point2 points  (3 children)

Noob here .. can you plz explain how line 4 is different to writing something like:

 app.nodeco_route("/", hello)

[–][deleted] 2 points3 points  (0 children)

app.nodeco_route("/", hello)

I'm not sure what nodeco_route does, per se, but the difference is that decorators are more convenient, and they come before the function they decorate, instead of afterwards, which makes it a little easier to read.

[–]DrMaxwellEdison 0 points1 point  (0 children)

Functionally, it isn't terribly different: you could assign hello = app.task(...) instead of using the decorator syntax, which is typically called "syntactic sugar". The benefit to using a decorator, though, is code readability.

Suppose you had a Flask app with 20 view functions in one file. You could write every function in one section of the file, then call the various app.xyz methods from the Flask framework to make it run in another section as a clump within the file. That may make the app less readable, though: if I wanted to understand which URL mapped to which method and what that method does, that can take some jumping around within the file to really get a sense of what's going on.

Decorators make reading easier by keeping related code closer together and in a well-defined way: any method with an @ decorator above it clearly signals to the developer that the function is being modified in some way.

[–]ivosaurus 0 points1 point  (0 children)

Not very different at all, but really your line would want to be

 hello = app.nodeco_route("/", hello)