假设我有一个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
第一个当然更短,但是其中一个比另一个更有效率吗?这些代码之间除了清晰之外还有其他区别吗?你知道吗
您的第二个示例更清楚地表达了您想要的内容,假设您确实希望
rest of code
被执行。你知道吗相关问题 更多 >
编程相关推荐