disclaimer: I'm obviously quite new to decorators
To create and decorate functions in python is quite simple and straight forward, and this excellent answer (the one with the most upvotes), gives a very good introduction and shows how to nest decorators. Well all this is fine and dandy. But what I've yet to figure out is how many of the (python)-web-frameworks (flask, django etc.) manage to call, what I can only guess is a decorated function based on arguments passed to the decorator.
An example (using Flask, but is similar in many frameworks) to show you what I mean.
@application.route('/page/a_page')
def show_a_page():
return html_content
@application.route('/page/another_page')
def show_another_page():
return html_content
Now if I make a request to mysite.com/page/a_page
flask somehow figures out that it should call show_a_page
, and ofcourse the same goes for show_another_page
if the request is to mysite.com/page/a_page
.
I'm wondering how I would go about implementing similar functionality in my own project?
I suppose that there exists something similar to using dir(module_name)
to extract information about the decoration(?) of each function?