为什么这段代码给我一个"IndentationError:意外的unindent"

gab*_*eio 1 python

下面的代码真的很烦我,我已经看了stackoverflow和谷歌但还没有找到任何东西,我是一个非常好的pyton程序员,还没有,至今,直到现在,发现一个我无法处理的错误.我已经尝试了所有的东西,但是代码的这种平和给了我IndentationError:意外的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)
Run Code Online (Sandbox Code Playgroud)

kin*_*all 9

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

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)
Run Code Online (Sandbox Code Playgroud)

或者,假设您正在application某处导入,您可以from application import route开始,然后您不需要任何if声明.