Google App Engine Python 代码:用户服务

2 投票
2 回答
1197 浏览
提问于 2025-04-15 11:44

这个来自Google App Engine文档的示例程序提到的self是什么意思呢?我可以在哪里查找像self.response这样的函数呢?

from google.appengine.api import users
from google.appengine.ext import webapp
from google.appengine.ext.webapp.util import run_wsgi_app

class MainPage(webapp.RequestHandler):
  def get(self):
    user = users.get_current_user()

    if user:
      self.response.headers['Content-Type'] = 'text/plain'
      self.response.out.write('Hello, ' + user.nickname())
    else:
      self.redirect(users.create_login_url(self.request.uri))

application = webapp.WSGIApplication(
                                     [('/', MainPage)],
                                     debug=True)

def main():
  run_wsgi_app(application)

if __name__ == "__main__":
  main()

2 个回答

3

在Python中,self是一个约定,意思类似于其他语言中的“this”,比如Java、C#、C++等等。我觉得在谈论对象时需要明确提到自己这点有点奇怪(因为我之前用的是Java),不过慢慢就习惯了。

如果你打算使用Python,我建议你找一个支持代码补全并且理解Python语法的编辑器,这样在查看某个类或模块可以用哪些函数时,会方便很多。

5

self 是指 webapp.RequestHandler 这个类。你可以在这里找到它的说明文档:http://code.google.com/appengine/docs/python/tools/webapp/requesthandlerclass.html,文档里会告诉你 response 的意思。

撰写回答