如何在Python函数中定义字符串?

发布于 2024-09-10 12:08:18 字数 1058 浏览 8 评论 0原文

我第一次使用 Python 和 Google App Engine,但无法在函数 (mytest2) 中定义字符串,声明后的行上出现缩进错误。我可以在有效的参数中定义一个(测试),但不明白为什么我也无法在函数中执行此操作。

我读了一些教程但没有获得启发。谁能让我知道我这里出了什么问题?

我还想知道如何通过在类中定义类似 mytest1 的内容然后在函数中访问来做到这一点?

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() 

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() 

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

指尖凝香 2024-09-17 12:08:18

永远不要在 python 中混合制表符和空格!

一般接受的做法是使用 4 个空格进行缩进。这是在 Python 风格指南 PEP 8 中编写的。我强烈推荐阅读它。

我通常将编辑器设置为用 4 个空格替换制表符,每个像样的文本编辑器都支持此操作。

制表符在您的示例中成为问题的原因是它们被最多 8 个空格替换,而您的缩进大部分为 4 个空格 (Python 文档)

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)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文