Return or Yield Outside Function
Documentation Index
Fetch the complete documentation index at: https://docs.sourcery.ai/llms.txt
Use this file to discover all available pages before exploring further.
Sourcery refactoring id: return-or-yield-outside-function
Section titled “Sourcery refactoring id: return-or-yield-outside-function”Description
Section titled “Description”Remove return or yield statements found outside function definitions.
Before
Section titled “Before”for i in range(10): print(i) return ifor i in range(10): print(i)Alternative
Section titled “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 yAfter:
def square(x): y = x**2 return yExplanation
Section titled “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.