views:

27

answers:

1

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.RequestHandler?

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

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.

Alex Martelli
So, I'm breaking encapsulation by subclassing... isn't this almost as dangerous as just reading the value directly in the first place?
Rosarch
This suggestion doesn't break encapsulation, and it isn't dangerous - you can rely on `debugmode` being there because you control that code (unlike trying to access `WSGIApplication`'s `__debug` field directly).
David Underhill
@Rosarch, I'm confused -- where ever did you read or otherwise "learn" that subclassing breaks encapsulation?! Subclassing is a fundamental thing in OOP and the subclass I suggested doesn't even peek at _any_ attribute of the base class, it just "captures" a copy of an `__init__` argument on the fly -- how could it **possibly** be **at all** "encapsulation-breaking"? Please clarify your worry.
Alex Martelli
@Alex looks like I read over your example too quickly. The method you use, of copying the example, is fine. What I thought you meant was "subclass the base, then use public getters for members that are protected in the base." (using Java terms there). The latter seems like a poor idea, but the former, which you suggested, makes sense.
Rosarch
@Rosarch, thanks for clarifying. If the attribute was "protected" (single underscore at the start of the name) it would be usefully accessible from subclasses, but the debug attribute is "private" (double underscore at the start of the name) which means "hands off, **every**body!", so of course using a subclass to access it would make no sense (and no difference) -- and my approach was, as you now see, a very different one. I think the exchange was instructive, so, thanks!
Alex Martelli