0

I'm using webapp2 and after browsing its source code I still can't figure out how *args and **kwargs arguments are passed to a RequestHandler object.

The code sample I'm looking at looks like this (the actual complete code source is from course-builder and can be found here ):

class ApplicationHandler(webapp2.RequestHandler):
    def __init__(self, *args, **kwargs):
        super(ApplicationHandler, self).__init__(*args, **kwargs)

Is there a way to know where these are coming from and how to debug them?

damienfrancois
  • 52,978
  • 9
  • 96
  • 110
vmussa
  • 71
  • 7
  • possible duplicate of [\*args and \*\*kwargs?](http://stackoverflow.com/questions/3394835/args-and-kwargs) – OdraEncoded Oct 22 '13 at 18:53
  • @OdraEncoded in fact what I want to know is not how *args and **kwargs work, but who instantiate an object and passes these arguments in a webapp2 web application. – vmussa Oct 22 '13 at 18:58

1 Answers1

0

*args means 'all positional arguments', and **kwargs means 'all keyword arguments'. The magic of it comes from the little stars (* and **), not the names (actually, it could be any name, for example the same effect would be achieved with *foo and **bar).

* and ** can be used in any function, not only constructors. The arguments are stored in the variable with the name given after the stars ; for instance, print args will print the list of positional arguments, and print kwargs will display the dictionary of keyword arguments.

In an inheritance case like the one in your question, it is quite common to call the base class with all the same arguments passed to the subclass constructor.

ApplicationHandler inherits from webapp2.RequestHandler and in the constructor it is calling the super class (base class) with the super keyword, passing to it all its arguments.

Let's study this example:

 class A:
   def __init__(self, arg1):
     self.my_value = arg1

 class B(A):
   def __init__(self, *args):
     super(B, self).__init__(*args)

 b = B("HELLO")

print b.my_value will display HELLO, because arguments passed to the B constructor have been passed to the A constructor in __init__. If you add print args in B.__init__, you will see ("HELLO", ).

mguijarr
  • 7,641
  • 6
  • 45
  • 72
  • Thanks for the answer, but who is the one that will do the "b = B("HELLO")" job in a web application environment, specifically in webapp2? Did I ask it right? – vmussa Oct 22 '13 at 19:02
  • Sorry I didn't understand your question... Normally request handlers are mapped to an URI, and get instantiated when the URI is loaded. "who" in your question is probably the WSGI application, I guess. To debug what is passed to it, just start with displaying args and kwargs... – mguijarr Oct 22 '13 at 19:08