Python logoPython/
PYL-E0104

`return` used outside of a functionPYL-E0104

Critical severityCritical
Bug Risk categoryBug Risk

Using a return statement only makes sense in the context of functions. Using it outside a function would raise a SyntaxError.

return in a loop would raise a SyntaxError:

for x in range(100):
    if x == 45:
        return
    print(x)

But, if this loop is inside a function, it is okay:

def my_func():
    for x in range(100):
        if x == 45:
            return
        print(x)