Return or Yield Outside Function¶
Sourcery refactoring id: return-or-yield-outside-function
¶
Description¶
Remove return or yield statements found outside function definitions.
Before¶
for i in range(10):
print(i)
return i
After¶
for i in range(10):
print(i)
Alternative¶
If you intend to return
or yield
some value, make sure that the statement is
correctly placed and indented in your code:
Before:
def square(x):
y = x**2
return y
After:
def square(x):
y = x**2
return y
Explanation¶
The
return
and
yield
statements can only be used inside function definitions. Using them outside this
context, or inside nested class definitions, is a
SyntaxError
.
This error may be very easy to pass through since sometimes it is caused by a wrong indentation.