In Python, should I use else after a return in an

2019-01-26 04:04发布

First, I couldn't find an answer to this in PEP 8. That doesn't mean it isn't in there. Feel free to point me at it.

Which style do you prefer?

The first one:

if spam:
    # Do stuff.
    return eggs
else:
    # Maybe do other stuff.
    return parrots

or the second one:

if spam:
    # Do stuff.
    return eggs
# Maybe do other stuff.
return parrots

8条回答
Rolldiameter
2楼-- · 2019-01-26 04:48

The second one. The same applies not only to return but also to break and continue statements. The else and the consequent indentation are eye-irritating and a waste of space.

查看更多
SAY GOODBYE
3楼-- · 2019-01-26 04:49

The second one! Less is more, and more is better.

查看更多
登录 后发表回答