Python Tornado - how can I fix the "URL that accepts X argument error"?

Here is the error:

TypeError: __init__() takes exactly 1 argument (3 given) ERROR:root:Exception in callback <tornado.stack_context._StackContextWrapper object at 0x1017d4470> Traceback (most recent call last): File "/Library/Python/2.7/site-packages/tornado-2.4.1-py2.7.egg/tornado/ioloop.py", line 421, in _run_callback callback() File "/Library/Python/2.7/site-packages/tornado-2.4.1-py2.7.egg/tornado/iostream.py", line 311, in wrapper callback(*args) File "/Library/Python/2.7/site-packages/tornado-2.4.1-py2.7.egg/tornado/httpserver.py", line 268, in _on_headers self.request_callback(self._request) File "/Library/Python/2.7/site-packages/tornado-2.4.1-py2.7.egg/tornado/web.py", line 1395, in __call__ handler = spec.handler_class(self, request, **spec.kwargs) TypeError: __init__() takes exactly 1 argument (3 given) 

And here is the code:

 class IndexHandler(tornado.web.RequestHandler): def __init__(self): self.title = "Welcome!" def get(self): self.render("index.html", title=self.title) 

I simplified the code to the above and I'm confused why this is causing this error. I have to do something wrong, but I have no idea that (3 arguments passed ??? ... mmmm?)

Note: the title variable is just <title>{{ title }}</title> in my index.html template.

I am running Python 2.7.3 in the 32-bit version to work with Mysqldb-Python. As you can see, my version of Tornado is 2.4.1. I also work on OSX Lion (if that matters ...) Perhaps a compatibility issue that ultimately causes this error?

Any help is appreciated when debugging. Thanks.

+4
source share
2 answers

@ The Princess of the Universe is correct, but perhaps this requires some development.

Tornado is going to call __init__ on RequestHandler subclasses with application, request, **kwargs parameters, so you need to enable this.

You can do it:

 def __init__(self, application, request, **kwargs): self.title = "Welcome!" super(IndexHandler, self).__init__(application, request, **kwargs) 

Which means that your IndexHandler class IndexHandler now initialized with the same signature as the parent class.

However, I would support the initialize method that Tornado provides for this purpose:

 def initialize(self): self.title = "Welcome!" 
+9
source

You redefine

 __init__() 

inappropriately.

Cm

http://www.tornadoweb.org/documentation/web.html

Signature

 class tornado.web.RequestHandler(application, request, **kwargs)[source] 

It is clear that you must provide the same API for the constructor of the derived class.

+1
source

All Articles