Skip to main content
 首页 » 编程设计

python之为什么这段代码给我一个 "IndentationError: unexpected unindent"

2025年05月04日9mfryf

下面的代码真的让我很烦 我已经查看了 stackoverflow 和谷歌但没有找到任何东西而且我是一个非常好的 pyton 程序员并且到目前为止还没有发现我无法处理的错误.我已经尝试了所有方法,但这种和平代码给了我 IndentationError: unexpected unindent 这很奇怪,因为正常错误是“意外缩进”,这意味着多次发布它的间距和我的间距它所以我检查了整个代码和 nada 相同的错误,我正确地输入了四个空格,但一切仍然……什么都没有。帮助?

from bottle import Bottle, run, route, static_file, debug 
from mako.template import Template as temp 
from mako.lookup import TemplateLookup 
 
lookup = TemplateLookup(directories=[base+'/templates']) 
application = Bottle() 
 
if __name__ == '__main__': 
    @route('/') 
else: 
    @application.route('/') 
def index(): 
    index_temp = lookup.get_template('index.html') 
    return index_temp.render(site=site, corperate=corperate, copyright=copyright) 

请您参考如下方法:

我认为您的想法是根据模块是直接运行还是导入,将不同的装饰器应用于函数。不幸的是,这不会像您所拥有的那样工作,因为装饰器调用需要紧随其后的函数。但是,您可以这样做:

if __name__ != '__main__': 
    route = application.route 
 
@route('/') 
def index(): 
    index_temp = lookup.get_template('index.html') 
    return index_temp.render(site=site, corperate=corperate, copyright=copyright) 

或者,假设您要在某处导入 application,您可以从 from application import route 开始,然后您将不需要任何 if 语句。