如果我在 WSGI 服务器上运行以下代码,则会引发错误,否则不会。
class Handler(webapp2.RequestHandler):
def __init__(self, template, *args, **kwargs):
print "Kwargs in Handler:", kwargs
self.template = template
super(Handler, self).__init__(*args, **kwargs)
class MainHandler(Handler):
def __init__(self, *args, **kwargs):
template = jinja_env.get_template("helloworld.html")
print "Kwargs in MainHandler:", kwargs
super(MainHandler, self).__init__(template= template, *args, **kwargs)
如果我在服务器上运行它,通过
from wsgiref.simple_server import make_server
server = make_server('localhost', 8000, app);
while True:
server.handle_request()
并试图访问localhost:8080
,我得到
Kwargs in MainHandler: {}
ERROR:root:__init__() got multiple values for keyword argument 'template'
Traceback (most recent call last):
File "/media/Data/code/kaiwarta/env/local/lib/python2.7/site-packages/webapp2.py", line 1535, in __call__
rv = self.handle_exception(request, response, e)
File "/media/Data/code/kaiwarta/env/local/lib/python2.7/site-packages/webapp2.py", line 1529, in __call__
rv = self.router.dispatch(request, response)
File "/media/Data/code/kaiwarta/env/local/lib/python2.7/site-packages/webapp2.py", line 1278, in default_dispatcher
return route.handler_adapter(request, response)
File "/media/Data/code/kaiwarta/env/local/lib/python2.7/site-packages/webapp2.py", line 1101, in __call__
handler = self.handler(request, response)
File "main.py", line 24, in __init__
super(MainHandler, self).__init__(template= template, *args, **kwargs)
TypeError: __init__() got multiple values for keyword argument 'template'
(请注意,kwargs
在传递给 Handler 之前它是空的。)
但是,如果我不启动服务器foo = MainHandler()
而是这样做,它会按预期运行:
Kwargs in MainHandler: {}
Kwargs in Handler: {}
第一种情况出了什么问题?