759
Comment: converted to 1.6 markup
|
759
Clarified Wording a bit
|
Deletions are marked like this. | Additions are marked like this. |
Line 1: | Line 1: |
Python raises a '''KeyError''' whenever a dict() object is requested a value using format {{{a = adict[key]}}} if key is not in the dictionary. |
Python raises a '''KeyError''' whenever a dict() object is requested. Using the format {{{a = adict[key]}}} if the key is not in the dictionary. |
Python raises a KeyError whenever a dict() object is requested. Using the format a = adict[key] if the key is not in the dictionary.
If you don't want to have an exception but default value used instead, you can use get() method:
Even more handy is somewhat controversially-named setdefault(key, val) which sets the value of key only if it is not already in the dict, and returns the value in any case: