gpt4 book ai didi

Python:NameError: 'self' 未定义

转载 作者:太空狗 更新时间:2023-10-29 22:01:46 25 4
gpt4 key购买 nike

我一定是在做傻事。我在 Google App Engine 中运行:

class MainHandler(webapp.RequestHandler):

def render(self, template_name, template_data):
path = os.path.join(os.path.dirname(__file__), 'static/templates/%s.html' % template_name)
self.response.out.write(template.render(path, template_data)) # error here

def get(self):
self.response.out.write("hi")

def main():
application = webapp.WSGIApplication([('/', MainHandler)],
debug=True)
util.run_wsgi_app(application)


if __name__ == '__main__':
main()

这给出了一个错误:

Traceback (most recent call last):
File "C:\Program Files\Google\google_appengine\google\appengine\tools\dev_appserver.py", line 3192, in _HandleRequest
self._Dispatch(dispatcher, self.rfile, outfile, env_dict)
File "C:\Program Files\Google\google_appengine\google\appengine\tools\dev_appserver.py", line 3135, in _Dispatch
base_env_dict=env_dict)
File "C:\Program Files\Google\google_appengine\google\appengine\tools\dev_appserver.py", line 516, in Dispatch
base_env_dict=base_env_dict)
File "C:\Program Files\Google\google_appengine\google\appengine\tools\dev_appserver.py", line 2394, in Dispatch
self._module_dict)
File "C:\Program Files\Google\google_appengine\google\appengine\tools\dev_appserver.py", line 2304, in ExecuteCGI
reset_modules = exec_script(handler_path, cgi_path, hook)
File "C:\Program Files\Google\google_appengine\google\appengine\tools\dev_appserver.py", line 2200, in ExecuteOrImportScript
exec module_code in script_module.__dict__
File "main.py", line 22, in <module>
class MainHandler(webapp.RequestHandler):
File "main.py", line 38, in MainHandler
self.writeOut(template.render(path, template_data))
NameError: name 'self' is not defined

我做错了什么?

最佳答案

定义类时发生异常,这意味着您的缩进已关闭。 Python 中的制表符相当于 8 个空格,因此如果前面的所有行都使用制表符并且制表位设置为 4 个空格,则缩进只是看起来正确。

关于Python:NameError: 'self' 未定义,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2963654/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com