A return statement, similar to a method call, needs parentheses around the return value, such as
Return statements need () around the return value
Return statements do not need () around the return value
CorrectionHere is what's right.
return statement is a special kind of statement, starting with the reserved word
return. That word is either followed by an expression that will evaluate to the value to return, or the word will stand alone with nothing following it. When
return is used on its own with no expression following it, it will leave the current function or method call with
None as its return value.
return is used in a
try statement with a
finally clause, the
finally clause is executed before leaving the function or method.
Placing the expression into parentheses is not necessary at all. Worse, it is confusing, because then the return statement looks like a call to a method named
return, which it is not.
SymptomsHow do you know your students might have this misconception?
The following example shows an occurrence of this misconception where only one of the three
return statements uses parentheses around the expression. The
return statements with literals don’t have parentheses, but the
return statement with a non-atomic expression surrounds that expression with parentheses.
def f(x): if x < 0: return 0 elif x == 0 or x == 1: return 1 else: return (f(x - 1) + 1)
A similar example, observed in a control-flow graph drawn by a student,
showed a node containing
but another node containing
In this case the student might also have wanted to ensure that the minus is seen as a unary operator
and not as an operand in a binary subtraction
(return - 1).