Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Trouble in understanding control flow in python class (in a google app engine)

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.)

enter image description here

like image 433
craftApprentice Avatar asked Apr 28 '12 15:04

craftApprentice


1 Answers

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:

from ..
from ..
class MainPage ..
application = ..
def main() ..
if __name__ ..

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 webapp and run_wsgi_app defined.

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: a def. Note that executing a def statement 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 to application.

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 the run_wsgi_app function, passing your already-built application value 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, a MainPage object 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 the MainPage object. The statements in the get method 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.

like image 77
Ned Batchelder Avatar answered Oct 21 '22 02:10

Ned Batchelder