正确的治疗方法,如果没有其他的,以d

2024-04-20 00:01:32 发布

您现在位置:Python中文网/ 问答频道 /正文

假设我有一个if子句在condition == True时做某事,而在False时什么也不做。我可以想出三种方法来表达它:省略else语句如下:

beginning of code
if condition == True:
     do something
rest of the code

明确告诉python什么也不做:

beginning of code
if condition == True:
     do something
else:
     pass
rest of the code

另一个可能不是很好的实践版本重复了代码的其余部分:

if condition == True:
     do something
     rest of the code
else:
     rest of the code

第一个当然更短,但是其中一个比另一个更有效率吗?这些代码之间除了清晰之外还有其他区别吗?你知道吗


Tags: ofthe方法代码restfalsetrueif