I am starting to learn how to use the google engine and, in most of the code I came across, they declare an instance of webapp.WSGIApplication as a global variable. This does not seem necessary, since the code works fine when it is locally declared in the main function. I have always been advised to avoid global variables. So is there a good or even not very good reason that it was so?
Example:
class Guestbook(webapp.RequestHandler): def post(self): greeting = Greeting() if users.get_current_user(): greeting.author = users.get_current_user() greeting.content = self.request.get('content') greeting.put() self.redirect('/') application = webapp.WSGIApplication([ ('/', MainPage), ('/sign', Guestbook)], debug=True) def main(): wsgiref.handlers.CGIHandler().run(application)
Why not do the following, which also works:
class Guestbook(webapp.RequestHandler): def post(self): greeting = Greeting() if users.get_current_user(): greeting.author = users.get_current_user() greeting.content = self.request.get('content') greeting.put() self.redirect('/') def main(): application = webapp.WSGIApplication([ ('/', MainPage), ('/sign', Guestbook)], debug=True) wsgiref.handlers.CGIHandler().run(application)
This also works in examples with multiple request handlers.
python google-app-engine global-variables
shafty
source share