dic.get(key[,fault]) ,当key不存在不会抛出异常,会显示fault信息
dic[key],当key不存在会抛出 KeyError 错误
>>> dic = {'name':'aaa','age':12}
>>> dic["name"]
'aaa'
>>> dic.get('name')
'aaa'
>>> dic['sex']
Traceback (most recent call last):
File "", line 1, in
dic['sex']
KeyError: 'sex'
>>> dic.get('sex')
>>> dic.get('sex','没有sex属性')
'没有sex属性'