设置问题 tornado.web.Application

issue in setting up tornado.web.Application

在龙卷风 (python) 中,我没有创建 tornado.web.Application() 的实例,而是尝试在 class 中进行更改,同时使用 init 调用它.

import tornado.web
import tornado.httpserver
import tornado.ioloop
import tornado.options
import os.path


from tornado.options import define, options
define("port", default=8000, help="run on the given port", type=int)


class MainHandler(tornado.web.RequestHandler):
    def get(self):
        self.render("index.html", page_title="My Bookstore | HOME", header_text="Welcome to My Bookstore!")


class Application(tornado.web.Application):
    def __init__(self):
        handlers = [
            (r'/', MainHandler),
        ]
        settings = dict(
            template_path = os.path.join(os.path.dirname(__file__), "templates"),
            static_path = os.path.join(os.path.dirname(__file__), "static"),
            debug = True
        )
        tornado.web.Application(self, handlers, **settings)


if __name__ == "__main__":
    tornado.options.parse_command_line()
    #Note: not creating an instance of application here ('app'), just creating a list of handlers and a dict of settings and passing it to the superclass.
    http_server = tornado.httpserver.HTTPServer(Application())
    http_server.listen(options.port)
    tornado.ioloop.IOLoop.instance().start()

但是我收到了这个错误,

Traceback (most recent call last):
  File "main.py", line 44, in <module>
    http_server = tornado.httpserver.HTTPServer(Application())
  File "main.py", line 27, in __init__
    tornado.web.Application(self, handlers, **settings)
  File "C:\Python\Python37\lib\site-packages\tornado\web.py", line 2065, in __init__
    handlers = list(handlers or [])
TypeError: 'Application' object is not iterable

错误是什么,我该如何解决?

当你初始化子class并想使用父class的__init__

时,你应该调用super

这个

tornado.web.Application(self, handlers, **settings)

应该是

super().__init__(handlers, **settings)