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

2024-06-25 06:17:06 发布

您现在位置: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

Tags: 答案returnifspamdoelseeggspep