⇤ ← Revision 1 as of 2005-09-14 10:11:27
759
Comment: Short explanation of KeyError
|
759
converted to 1.6 markup
|
No differences found! |
Python raises a KeyError whenever a dict() object is requested a value using format a = adict[key] if 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: