9

How can I create initialization code? When I put the __init__ constructor always tell me that parameters are wrong.

Also please someone gave a example also using __new__ and one using super() and why should we use or not use them.

import webapp2

class MainHandler( webapp2.RequestHandler ):
    def __init__( self ):
        #initialization code/vars
        x = 1

    def get( self ):
        #code for get here
        self.response.write( x+1 )

    def post( self ):
        #code for post here
        self.response.write( x+2 )

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

ZEE
  • 2,931
  • 5
  • 35
  • 47

3 Answers3

14

Finally got it... The problem is that overriding "webapp2.RequestHandler" requires special special handling

from the webapp2 manual:

If you want to override the webapp2.RequestHandler.init() method, you must call webapp2.RequestHandler.initialize() at the beginning of the method. It’ll set the current request, response and appobjectsasattributesofthehandler. Example:

class MyHandler(webapp2.RequestHandler):
    def __init__(self, request, response):
    # Set self.request, self.response and self.app.
    self.initialize(request, response)
    # ... add your custom initializations here ...
    # ...

...and that's it... now works as expected ;-)

ZEE
  • 2,931
  • 5
  • 35
  • 47
  • It's probably obvious, but I had to double-check - `self.initialize(request, response)` won't call any parent handlers' constructors, so you will need to explicitly do that (and move `self.initialize(request, response)` into the parent). Call the parent with `super(subclassNameHere, self).__init__(request, response)`. – Sam Aug 17 '16 at 10:50
2

If you aren't passing any arguments or including any of your own code in the __init__ method, there's usually no need to even create one. You'll just use webapp2.RequestHandler's __init__ method.

If you do need to make one, you still have to call webapp2.RequestHandler.__init__:

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

        self.your_arg = your_arg
Blender
  • 289,723
  • 53
  • 439
  • 496
  • I'm using webapp2 and code is this ----> class theHandler( webapp2.RequestHandler ): def __init__( self ): #def some class shared vars (...) the error is ----> TypeError: __init__() takes exactly 1 argument (3 given) INFO 2013-03-13 23:30:43,937 dev_appserver.py:3104] "GET /rotx HTTP/1.1" 500 - – ZEE Mar 13 '13 at 23:35
  • Sorry for the confusing text... the error is : TypeError: __init__() takes exactly 1 argument (3 given) INFO 2013-03-13 23:30:43,937 dev_appserver.py:3104] "GET /rotx HTTP/1.1" 500 - – ZEE Mar 13 '13 at 23:43
  • @ZEE: Post your actual class definition and how you're initializing it. – Blender Mar 13 '13 at 23:43
  • Just updated the code very simplified just to expose the issue... i'm sure i'm missing something here... – ZEE Mar 13 '13 at 23:51
  • @ZEE: You have to read through a Python tutorial. You're missing out on a lot of core concepts: http://learnpythonthehardway.org/book/ – Blender Mar 13 '13 at 23:54
0

You need to have a self variable in all the functions within your class. You need to include this variable for the function to work within your class.

A good explanation to the need of the self variable in each function within a class can be found here.

Community
  • 1
  • 1
eandersson
  • 25,781
  • 8
  • 89
  • 110