views:

533

answers:

2

What does this example program from the Google App Engine documentation mean when it references self? Where can i look up what methods (such as 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()
+4  A: 

self refers to the webapp.RequestHandler class. Here is its documentation: http://code.google.com/appengine/docs/python/tools/webapp/requesthandlerclass.html, which tells you what response means.

pts
but webapp.RequestHandler is the parameter for the class MainPage...I don't understand why the MainPage object wouldn't be what is being referenced when "self" is called.
Devoted
MainPage /is/ the type of the self object, but it inherits from webapp.RequestHandler, which is where properties like 'response' are defined. You need to look at the Webapp reference docs (within the appengine docs).
Ben Collins
Devoted, "class MainPage(webapp.RequestHandler):" is Python syntax for MainPage inherits from webapp.RequestHandler; therefore, methods available in webapp.RequestHandler are also available to MainPage.
allyourcode
+2  A: 

self is a python convention which means 'this' in other languages like Java, C#, C++, etc...I've found it bizarre that you need to explicitly reference yourself when talking about objects (I have a Java background), but you sort of get used to it.

If you're going to use python, I suggest you get an editor that does code completion and understands python syntax, it'll make your life easier when trying to determine what functions are available for a given class or module

Chris Gow