错误:unindent与任何外部缩进级别都不匹配

时间:2014-05-25 18:38:43

标签: python bots reddit praw

该代码适用于reddit上的bot。

问题似乎在这里:

@validatedForm(VLoggedOut() 
               user = VThrottledLogin(['user', 'hunter2']),
               rem = VBoolean('rem'))
               def _handle_login(self, form, responder, user, rem):
               exempt_ua = (request.user_agent and
               any(ua in request.user_agent for ua
               in g.config.get('exempt_login_user_agents', ())))

1 个答案:

答案 0 :(得分:1)

由于解释器无法确定您的代码有多少缩进,或者您通过函数行混合了制表符和空格,因此IndentationError被引发。例如,这会引发IndentationError

def onPurpose(self):
[Tab][Space]foo()
[Space][Space][Space][Space][Space]bar()

此处,[Tab]表示由[Tab]按钮创建的空格,[Space]是使用[Space]按钮创建的空格。对于某些编辑器1 tab = 4 spaces而言,Python无法确定应在bar()函数中运行onPurpose。这是一个正确的版本:

def onPurpose(self):
[Tab]foo()
[Tab]bar()

我的建议:

  1. 尝试不要混合使用空格和制表符,它们会使Python和你头晕目眩以确定代码应该如何运行。
  2. 使用专用的源代码编辑器(如Notepad ++)来解决您的标签问题。
  3. 使用等宽字体。
  4. 更好地使用软标签(将标签转换为空格,通常为4)而不是硬标签,无论您使用哪种编辑器,它们都会看起来更流畅。
相关问题