简体   繁体   English

Python错误:“返回”外部函数

[英]Python error: 'return' outside function

I'm having a problem with python and I cannot figure out why is it happening. 我在使用python时遇到问题,无法弄清楚为什么会发生这种情况。

My code is the following: 我的代码如下:

def getServers(baseDN=''):
    from pyad import adquery
    q = adquery.ADQuery()
    q.execute_query(
        attributes=["distinguishedName", "description"], \
        where_clause="objectClass = 'Computer'", \
        base_dn=baseDN)
    #Lo que devuelve es un generator campeon :) Podes pasarlos con next o con un for.
    return [server['distinguishedName'].split(',')[0].strip('CN=') for server in q.get_results()] # Y aca lo parseo con un for

So basically, if I execute this code by executing a py file it works perfectly. 因此,基本上,如果我通过执行py文件来执行此代码,则效果很好。 However, if I try to use it on a console I get a "return outside function" error, and I cannot figure out how to solve it as I'm trying to debug my code by running pieces on the console :( 但是,如果尝试在控制台上使用它,则会出现“返回外部函数”错误,并且由于在尝试通过在控制台上运行代码来调试代码而无法解决该问题:(

Thanks for your help :) 谢谢你的帮助 :)

The parser thinks that the function is over before the return statement, which means that there is probably some kind of problem with your indentation. 解析器认为函数在return语句之前结束,这意味着缩进可能存在某种问题。 Make sure that you have the same number of spaces/tabs before each line (or make sure you're not mixing spaces and tabs, spaces on some lines, tabs on others). 确保每行之前有相同数量的空格/制表符(或确保您没有混合使用空格和制表符,某些行中的空格或其他行中的制表符)。 It might also help to get rid of the commented line. 这也可能有助于摆脱注释行。

Edit: The fact that removing the comment fixed your problem would indicate that the console you are using is defective, since the syntax is correct. 编辑:删除注释可解决您的问题的事实将表明您使用的控制台有缺陷,因为语法正确。

The problem was the comment before the return. 问题是归还之前的评论。 The console for some reason was not understanding the continuation very well. 控制台由于某种原因不能很好地理解延续性。 As I was interested just in testing the code I simply removed the comment. 因为我只是对测试代码感兴趣,所以我删除了注释。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM