Issue
With pylint, I know that R1705 warning gets triggered when you put a 'return' inside an 'else'.
This is the warning:
R1705: Unnecessary "else" after "return" (no-else-return)
This is what the docs says about it:
Unnecessary “else” after “return” Used in order to highlight an unnecessary block of code following an if containing a return statement. As such, it will warn when it encounters an else following a chain of ifs, all of them containing a return statement.
A snippet of code that will trigger R1705:
if CONDITION1:
return something1
else:
return something2
The desired fix to shut down the warning:
if CONDITION1:
return something1
return something2
Is it really needed to obey this? What's the benefit? I mean I understand that after returning something from a function there is no way to come back and read further code.
But I find it way more organized to use 'else'.
Solution
If you're trying to conform to Mozilla Coding Style or similar then R1705 makes sense. Quoting:
Don't put an else right after a return (or a break). Delete the else, it's unnecessary and increases indentation level.
Otherwise, you might prefer to disable that warning.
Better still, consider switching to flake8
,
which tends to stay pretty silent if you've been writing sensible code.
Outside of the Mozilla community,
most folks would rather see simple parallel functional clauses
handled with an else
, like this:
def max(a, b):
if a > b:
return a
else:
return b
Answered By - J_H
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.