Python dict.get() raises KeyError

后端 未结 2 715
深忆病人
深忆病人 2021-01-13 12:11

I am getting lost here, Python 2.7, I have a dictionary mt, and I use the get() method, which by documentation says:

g

2条回答
  •  失恋的感觉
    2021-01-13 12:21

    >>> mt = {'key1' : 1}
    >>> mt.get('is_rebill', 0)
    0
    

    It does not generates key error if key is not present it returns 0

    >>> mt.update({'is_rebill':1})
    >>> mt.get('is_rebill', 0)
    1
    
    >>> if mt.get('is_rebill', 0) == 1:
    ...     print True
    ... else:
    ...     print False
    ... 
    False
    

提交回复
热议问题