Using try vs if in python

后端 未结 9 1113
终归单人心
终归单人心 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:29

    Your second example is broken - the code will never throw a TypeError exception since you can iterate through both strings and lists. Iterating through an empty string or list is also valid - it will execute the body of the loop zero times.

提交回复
热议问题