Why dict.get(key) instead of dict[key]?

后端 未结 10 2820
生来不讨喜
生来不讨喜 2020-11-21 05:51

Today, I came across the dict method get which, given a key in the dictionary, returns the associated value.

For what purpose is this funct

10条回答
  •  夕颜
    夕颜 (楼主)
    2020-11-21 06:27

    The purpose is that you can give a default value if the key is not found, which is very useful

    dictionary.get("Name",'harry')
    

提交回复
热议问题