Guys, I’m having some trouble in understanding control flow in python class, i.e., what is happing with the code step by step. Given the short code bellow, I’d like to know: when the class MainPage is called it just executes every thing that is inside that class? In linear order? (the first line, after the second etc.)

When a Python file is executed, every statement in the file is executed from the top to the bottom. In your case, there are six statements:
The first two find other Python modules, and execute all of their statements, which likely just define a bunch of classes, and then define some names in your module using values from those other modules. So after the first two statements, we have
webappandrun_wsgi_appdefined.The third statement defines the class
MainPage. It does this by executing the statements inside the class body, in this case there is only one: adef. Note that executing adefstatement doesn’t execute the statements in the function body, it just collects those statements into a named function. When the class body ends, all the names defined become attributes of the class.The fourth statement calls
webapp.WSGIApplication, and assigns the result toapplication.The fifth statement defines a function called
main.The sixth statement looks at the name
__name__. Every time a Python file is executed, it is given a__name__variable. If the file is the main one being run, that is, the one Python started with, the value of__name__is"__main__". This if statement is an idiom meaning, “Am I the main program?” In this case, it is, so the body of the if statement is also executed.The body of the if simply calls the
main()function, which itself invokes therun_wsgi_appfunction, passing your already-builtapplicationvalue to it.Running a WSGI app is involved, but basically amounts to, “wait until someone visits a URL, and then map the URL to some code, and then execute the code.” In your case, you provided a URL map that associated “/” with
MainPage. Once someone visits the / URL, aMainPageobject will be created, and a method on it will be invoked.In particular, because the HTTP method used to visit / was GET, the WSGI app runner calls the
.get()method on theMainPageobject. The statements in thegetmethod are executed in sequence, writing some data to the response. When there are no more statements in the function body, it returns. The WSGI application container sends that response back to the web browser!This program never ends, because the WSGI application runner loops forever waiting for the next URL visit. But if it didn’t, once the last statement in your main file completed, the Python program would be done, and the process would end.