tags:

views:

170

answers:

3

This is a snippet from Google AppEngine tutorial.

application = webapp.WSGIApplication([('/', MainPage)], debug=True)

I'm not quite sure what debug=True does inside the constructor call. Does it create a local variable with name debug, assign True to it, and pass it to constructor, or is this a way to set a class instance member variable's value in constructor?

+3  A: 

It's using named arguments.

Idan K
+4  A: 

Neither -- rather, webapp.WSGIApplication takes an optional argument named debug, and this code is passing the value True for that parameter.

The reference page for WSGIApplication is here and it clearly shows the optional debug argument and the fact that it defaults to False unless explicitly passed in.

As the page further makes clear, passing debug as True means that helpful debugging information is shown to the browser if and when an exception occurs while handling the request.

How exactly that effect is obtained (in particular, whether it implies the existence of an attribute on the instance of WSGIApplication, or how that hypothetical attribute might be named) is an internal, undocumented implementation detail, which we're not supposed to worry about (of course, you can study the sources of WSGIApplication in the SDK if you do worry, or just want to learn more about one possible implementation of these specs!-).

Alex Martelli
+11  A: 

Python functions accept keyword arguments. If you define a function like so:

def my_func(a, b='abc', c='def'):
    print a, b, c

You can call it like this:

my_func('hello', c='world')

And the result will be:

hello abc world

You can also support dynamic keyword arguments, using special syntax:

def my_other_func(a, *b, **c):
    print a, b, c
  • *b means that the b variable will take all non-named arguments after a, as a tuple object.
  • **c means that the c variable will take all named arguments, as a dict object.

If you call the function like this:

my_other_func('hello', 'world', 'what a', state='fine', what='day')

You will get:

hello ('world', 'what a') {'state': 'fine', 'what': 'day'}
Blixt
Thanks for the quick response!!! This is apparently a better way of overriding a default argument value than how c/c++ does, where I will have to also pass b='abc' in your example.
Kei
Indeed, that is one of the uses of named arguments =)
Blixt
Compare/Take a look also at Ada language: also Ada had named arguments
daitangio