2016/01/13

[Google App Engine][Python]dispatchメソッドについて

各methodの処理前に共通処理を行いたい場合は、dispatchメソッドが使えるみたい。

class webapp2_extras.sessions.SessionStore(request, config=None)
に書かれているサンプルを見ながら下のように実行してみた。

import webapp2

class MainHandler(webapp2.RequestHandler):
    def dispatch(self):
      self.response.write("Hello dispatch")
      webapp2.RequestHandler.dispatch(self)

    def get(self):
      self.response.write("Hello world")

app = webapp2.WSGIApplication([
  ('/',MainHandler)
],debug=True)
これ実行したら、確かに、Hello dispatchが先に表示された。

下のようにしても同じ結果を得ることができた。
import webapp2

class MainHandler(webapp2.RequestHandler):
    def dispatch(self):
      self.response.write("Hello dispatch")
      super(MainHandler,self).dispatch()

    def get(self):
      self.response.write("Hello world")

app = webapp2.WSGIApplication([
  ('/',MainHandler)
],debug=True)

0 コメント:

コメントを投稿