小编典典

在Python中,是否应该在if块返回后使用else?

python

首先,我在PEP
8中
找不到答案。那并不意味着它不在那儿。请随时指出我的意思。

您喜欢哪种风格?

第一个

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

第二个

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

阅读 142

收藏
2021-01-16

共1个答案

小编典典

这取决于,如果您将返回鹦鹉,当您不返回鸡蛋时,第一个更加清晰。但是,如果您尝试捕获错误或类似的错误,则第二个更好。

2021-01-16