Using try vs if in python

后端 未结 9 1022
终归单人心
终归单人心 2020-11-22 12:59

Is there a rationale to decide which one of try or if constructs to use, when testing variable to have a value?

For example, there is a f

9条回答
  •  隐瞒了意图╮
    2020-11-22 13:26

    As a general rule of thumb, you should never use try/catch or any exception handling stuff to control flow. Even though behind the scenes iteration is controlled via the raising of StopIteration exceptions, you still should prefer your first code snippet to the second.

提交回复
热议问题