Python performance: Try-except or not in?

前端 未结 5 785
南笙
南笙 2020-12-19 19:32

In one of my classes I have a number of methods that all draw values from the same dictionaries. However, if one of the methods tries to access a value that isn\'t there, it

5条回答
  •  温柔的废话
    2020-12-19 20:11

    I believe the .get() method of a dict has a parameter for setting the default value. You could use that and have it in one line. I'm not sure how it affects performance though.

提交回复
热议问题