`if key in dict` vs. `try/except` - which is more readable idiom?

前端 未结 10 2506
陌清茗
陌清茗 2020-11-28 06:19

I have a question about idioms and readability, and there seems to be a clash of Python philosophies for this particular case:

I want to build dictionary A from dict

10条回答
  •  心在旅途
    2020-11-28 06:45

    I think the general rule here is will A["blah"] normally exist, if so try-except is good if not then use if "blah" in b:

    I think "try" is cheap in time but "except" is more expensive.

提交回复
热议问题