views:

71

answers:

1

Hi,

I'm playing with Python and Google App Engine for the first time but am unable to define a string within my function (mytest2), I get an indentation error on the line after the declaration. I can define one in the parameters that works (test) but can't see why I wouldn't be able to do it in the function aswell.

I've read a few tutorials without gaining enlightenment. Can anyone let me know what I've got wrong here?

I'm also wondering how I could do this by having something like mytest1 defined in the class and then accessible within the function?

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

class MainPage(webapp.RequestHandler):
    #mytest1 = "test1" 
    #Runtime failure on this line talking about an indent issue
    def get(self, test = 'testing!'):
        mytest2= "test2" #Runtime failure on this line talking about an indent issue
        self.response.headers['Content-Type'] = 'text/plain'
        self.response.out.write('Hello, webapp World!\n')
        self.response.out.write(self)
        #self.response.out.write('\n' + mytest1)
        self.response.out.write('\n' + mytest2)

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

def main():
    run_wsgi_app(application)

if __name__ == "__main__":
    main() 
+2  A: 

Never mix tabs and spaces in python!

Generally accepted practice is to use 4 spaces for indentation. This is written in PEP 8 , the python style guide. I strongly recommmend reading it.

I usually set my editor to replace tabs with 4 spaces, every decent text editor supports this.

The reason why tabs are a problem in your example is that they are replaced by up to 8 spaces and your indentation is mostly 4 spaces (Python documentation)

Fabian