I'm a beginner and am wondering why we use self.response.out.write instead of print, and why we use classes, instead of functions, for the request handlers in the first place. Are there any special reasons?
Why does GAE use classes and self.response.out.write over functions and print?
767 Views Asked by Han At
1
There are 1 best solutions below
Related Questions in PYTHON
- Does compiler optimize operation on const variable and literal const number?
- Optimizing for Social Leaderboards
- 3D FFT with data larger than cache
- Optimum directory structure for large number of files to display on a page
- How to make faster queries on my mysql table?
- Xib taking long time (>1s) to load. UIFont cache seems to blame
- How to speed up string comparisons in an array with a for loop?
- How to load all symbols from shared library on start up?
- Cython speed vs numpy
- Improve Speed of Piecewise Function in MATLAB
Related Questions in GOOGLE-APP-ENGINE
- Does compiler optimize operation on const variable and literal const number?
- Optimizing for Social Leaderboards
- 3D FFT with data larger than cache
- Optimum directory structure for large number of files to display on a page
- How to make faster queries on my mysql table?
- Xib taking long time (>1s) to load. UIFont cache seems to blame
- How to speed up string comparisons in an array with a for loop?
- How to load all symbols from shared library on start up?
- Cython speed vs numpy
- Improve Speed of Piecewise Function in MATLAB
Related Questions in WEBAPP2
- Does compiler optimize operation on const variable and literal const number?
- Optimizing for Social Leaderboards
- 3D FFT with data larger than cache
- Optimum directory structure for large number of files to display on a page
- How to make faster queries on my mysql table?
- Xib taking long time (>1s) to load. UIFont cache seems to blame
- How to speed up string comparisons in an array with a for loop?
- How to load all symbols from shared library on start up?
- Cython speed vs numpy
- Improve Speed of Piecewise Function in MATLAB
Trending Questions
- UIImageView Frame Doesn't Reflect Constraints
- Is it possible to use adb commands to click on a view by finding its ID?
- How to create a new web character symbol recognizable by html/javascript?
- Why isn't my CSS3 animation smooth in Google Chrome (but very smooth on other browsers)?
- Heap Gives Page Fault
- Connect ffmpeg to Visual Studio 2008
- Both Object- and ValueAnimator jumps when Duration is set above API LvL 24
- How to avoid default initialization of objects in std::vector?
- second argument of the command line arguments in a format other than char** argv or char* argv[]
- How to improve efficiency of algorithm which generates next lexicographic permutation?
- Navigating to the another actvity app getting crash in android
- How to read the particular message format in android and store in sqlite database?
- Resetting inventory status after order is cancelled
- Efficiently compute powers of X in SSE/AVX
- Insert into an external database using ajax and php : POST 500 (Internal Server Error)
Popular # Hahtags
Popular Questions
- How do I undo the most recent local commits in Git?
- How can I remove a specific item from an array in JavaScript?
- How do I delete a Git branch locally and remotely?
- Find all files containing a specific text (string) on Linux?
- How do I revert a Git repository to a previous commit?
- How do I create an HTML button that acts like a link?
- How do I check out a remote Git branch?
- How do I force "git pull" to overwrite local files?
- How do I list all files of a directory?
- How to check whether a string contains a substring in JavaScript?
- How do I redirect to another webpage?
- How can I iterate over rows in a Pandas DataFrame?
- How do I convert a String to an int in Java?
- Does Python have a string 'contains' substring method?
- How do I check if a string contains a specific word?
Using Methods
Each handler class has methods with names like
getandpost, after the HTTP methodsGETandPOSTetc. Those methods are functions that handle requests.Each request to your server will be routed to a request handler object, which is a new instance of some request handler class. So, a request handler instance is created per request, and is garbage collected once its HTTP response is sent.
By inheriting from
webapp2.RequestHandler, your handler classes get a bunch of functionality out the box for free. For example, handler instances will have the data from the HTTP request parsed into dictionaries and bound toselfasself.request.headersandself.request.bodyautomatically.The
webapp2.RequestHandlerclass also providesself.response, which is what you write your response data to.Once the new request handler instance is initialised, the inherited
__init__method calls the method that maps to the HTTP request method, so assuming aGETrequest, it callsself.get. Thewebapp2.RequestHandlerclass doesn't implement those methods; your derived class does.Responding
Neither
printnor the return value of the handler method are used here. You do not 'return a response' with this framework; you write the response to the request handler instance's (inherited)self.responseproperty.Your instance inherits
self.response.out.write(which is aliased toself.response.write), which concatenates its argument to the body of the response, initially an empty string.Note: You can call
self.response.clearto clear the response body.When you return from your handler method -
getorpostetc. - the return value is ignored. The framework uses the state ofself.responseto automatically create and send a HTTP response for you.There's a bunch of subtleties that the framework takes care of behind the scenes too.
Classes Over Functions
The main advantage is in inheritance. Normally, you'll create a single
BaseHandlerclass that derives fromwebapp2.RequestHandler. TheBaseHandlerclass will contain the core functionality for your actual handlers. It might include some logic for converting data into little JSON packages for a Web API, for example. All of the classes that actually handle requests would then be derived from yourBaseHandler.You want a custom base class for your handler classes to derive from mainly so you can edit that base class. You want that base class to inherit from
webapp2.RequestHandlerso that all your handler instances inherit the framework magic.There is enough slight of hand to make the whole thing confusing, but it is easy to make sense of once you get it, and does save a lot of trouble.
Technically, you could achieve all of the above just using functions and dictionaries, but Python is classically object oriented, so it would be painful and weird.