开发者

Google App Engine/WSGIApplication: How to check debug?

In the WSGIApplication's constructor, it takes a debug argument. Is there a way to access the value set for this from the the handler classes that inherit from webapp.R开发者_如何转开发equestHandler?

def main():
    application = webapp.WSGIApplication([('/', fooHandler)
                                          ],
                                        debug=True)
    util.run_wsgi_app(application)


A WSGIApplication instance records the value of the debug parameter as self.__debug: the double underscore is a strong indication that no code outside the class itself is supposed to look at this attribute, as it's considered an internal application detail and could change "at any time" (even in a minor revision of the API). If you want to ignore this extremely strong indication, you could, technically, use webapp.WSGIApplication.active_instance._WSGIApplication__debug to look at it, but it's a truly bad idea.

A much better idea is to subclass WSGIApplication in your own code to make the attribute publically visible:

class MyWSGIapp(webapp.WSGIApplication):
    def __init__(self, url_mapping, debug=False):
        self.debugmode = debug
        webapp.WSGIApplication.__init__(self, url_mapping, debug)

Now, when you use MyWSGIapp instead of webapp.WSGIApplication to start things off, webapp.WSGIApplication.active_instance.debugmode becomes a clean, solid way to access the attribute of interest from wherever else in your application.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜