简体   繁体   中英

Python error: 'return' outside function

I'm having a problem with python and I cannot figure out why is it happening.

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. 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. 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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