Unnecessary else statements

Опубликовано: 28 Февраль 2026
на канале: Python Morsels
648
38

When your function ends in an "else" block with a "return" statement in it, should you remove that "else"?

Also see related pylint rule R1705 (no-else-return) or ruff rule RET505 (superfluous-else-return), which I don't recommend always following.

And note that the example in the code could use a ternary operator, though it might be a long one!:    • Python's ternary operator  

Article at https://pym.dev/unnecessary-else-stat...

More #Python screencasts at https://pym.dev/screencasts/

00:00 A function where both if and else return
00:20 Is that else statement unnecessary?
00:30 Sometimes else improves readability
01:12 When should you remove an else statement?
01:58 Considering readability with if-else statements