Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/287.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 为什么这段代码给我一个“缩进错误:意外的未缩进”_Python - Fatal编程技术网

Python 为什么这段代码给我一个“缩进错误:意外的未缩进”

Python 为什么这段代码给我一个“缩进错误:意外的未缩进”,python,Python,下面的代码真的让我很恼火,我已经查看了stackoverflow和google,但还没有发现任何东西,我是一个相当好的pyton程序员,到目前为止,还没有发现一个我无法处理的错误。我已经尝试了所有的方法,但是这种代码的平静给了我缩进错误:意外的未缩进,这很奇怪,因为正常的错误是未预期的缩进,这意味着发布了很多次,它是间隔,我是如何间隔的,所以我通读了整个代码,发现了一个相同的错误,我正确地放入了四个空格,所有的东西都没有改变... 没有什么帮忙 from bottle import Bottle

下面的代码真的让我很恼火,我已经查看了stackoverflow和google,但还没有发现任何东西,我是一个相当好的pyton程序员,到目前为止,还没有发现一个我无法处理的错误。我已经尝试了所有的方法,但是这种代码的平静给了我缩进错误:意外的未缩进,这很奇怪,因为正常的错误是未预期的缩进,这意味着发布了很多次,它是间隔,我是如何间隔的,所以我通读了整个代码,发现了一个相同的错误,我正确地放入了四个空格,所有的东西都没有改变... 没有什么帮忙

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)

我对您使用装饰器的方式感到困惑,请查看以下内容:


我相当肯定,您对装饰器的使用需要与函数定义本身处于相同的缩进级别。另外,第一个decorator@route没有后续功能。

我对您使用decorator的方式感到困惑,请查看以下内容:

我相当肯定,您对装饰器的使用需要与函数定义本身处于相同的缩进级别。此外,第一个decorator@route之后没有任何函数。

不允许您尝试执行的操作。改为这样做:

def index():
    index_temp = lookup.get_template('index.html')
    return index_temp.render(site=site, corperate=corperate, copyright=copyright)

if __name__ == '__main__':
    index = route('/')(index)
else:
    index = application.route('/')(index)
不允许您尝试做的事情。改为这样做:

def index():
    index_temp = lookup.get_template('index.html')
    return index_temp.render(site=site, corperate=corperate, copyright=copyright)

if __name__ == '__main__':
    index = route('/')(index)
else:
    index = application.route('/')(index)

我认为您的想法是根据模块是直接运行还是导入,对函数应用不同的修饰符。不幸的是,这不会按您的方式工作,因为decorator调用需要紧跟其后的函数。但是,您可以这样做:

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)

或者,假设您正在某个地方导入应用程序,您可以从应用程序导入路径开始,然后就不需要任何if语句。

我认为您的想法是根据模块是直接运行还是导入,对函数应用不同的装饰器。不幸的是,这不会按您的方式工作,因为decorator调用需要紧跟其后的函数。但是,您可以这样做:

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)

或者,假设您要在某个地方导入应用程序,您可以从应用程序导入路径开始,然后就不需要任何if语句。

您不能以这种方式将装饰器从装饰函数中分离出来

您仍然可以计算装饰器,然后应用它:

if condition:
  deco = route('/')
else:
  deco = application.route('/')
@deco
def foo(...):

不能用这种方式将装饰器从装饰函数中分离

您仍然可以计算装饰器,然后应用它:

if condition:
  deco = route('/')
else:
  deco = application.route('/')
@deco
def foo(...):

你可以这样做

route_decorator = route if __name__ == '__main__' else application.route

@route_decorator('/')
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)

你可以这样做

route_decorator = route if __name__ == '__main__' else application.route

@route_decorator('/')
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)

另一种可能的解决方案接近您现有的解决方案:

def decorate(func):
    if __name__ == '__main__':
       @route('/')
       def f():
          func()
    else:
       @application.route('/')
       def f():
          func()

    return f

@decorate
def index():
    index_temp = lookup.get_template('index.html')
    return index_temp.render(site=site, corperate=corperate, copyright=copyright)

另一种可能的解决方案接近您现有的解决方案:

def decorate(func):
    if __name__ == '__main__':
       @route('/')
       def f():
          func()
    else:
       @application.route('/')
       def f():
          func()

    return f

@decorate
def index():
    index_temp = lookup.get_template('index.html')
    return index_temp.render(site=site, corperate=corperate, copyright=copyright)

我不认为你可以在这样的if语句中加入一个装饰师。这可能是问题所在吗?具体地说,它希望在if块中有一个函数定义,因此else意外地不包含。我不认为你可以在if语句中像那样放置一个decorator。这可能是问题所在吗?具体来说,它希望在if块中有一个函数定义,因此else意外地没有包含。我更新了完整的代码,不知道是否能够在修饰的代码周围放置和if语句。。。@route是url的路由的位置,如本例中的@route'/'指向localhost/No的索引,我认为您不能。它在您的第一个decorator之后需要一个函数定义,看到else:它不像它期望的函数定义那样缩进,并抛出异常。我更新了完整的代码,我不知道我是否真的能够在一个修饰的语句周围放置和if语句。。。@route是url的路由的位置,如本例中的@route'/'指向localhost/No的索引,我认为您不能。它在您的第一个decorator之后需要一个函数定义,看到else:没有像它所期望的函数定义那样缩进,并抛出异常。那么,是否不能将decorator放在if语句中?@C Genius Go:如果没有它们所附加的函数,就不能。您可以在if调用中设置decorator函数,然后将其用作decorator。将if语句放入您自己的decorator中!编写一个decorator函数并不难,@-语法要求@后面是一个点的_名称,还可以选择一个由paren括起来的参数列表。它不能是一个表达式或语句。那么,在if语句中是否不可能放置decorator呢?@C Genius Go:如果没有它们所附加的函数,就不能。您可以在if调用中设置decorator函数,然后将其用作decorator。将if语句放入您自己的decorator中!编写一个decorator函数并不难,@-语法要求@后面是一个点的_名称,并且可选 由参数括起来的参数表。它不能是表达式或语句。