[Tutor] KeyError: 'DEFAULT'

Kent Johnson kent37 at tds.net
Thu Feb 19 21:42:04 CET 2009


Oops, hit send by mistake...continued below.

On Thu, Feb 19, 2009 at 3:40 PM, Kent Johnson <kent37 at tds.net> wrote:
> On Thu, Feb 19, 2009 at 2:34 PM, Isaac Eiland-Hall <daychilde at gmail.com> wrote:
>> http://python.pastebin.com/m26864a1b
>>
>>
>>
>> Traceback (most recent call last):
>>
>>                 File "./loopy", line 328, in <module>
>>
>>
>> set[current_set][current_section][current_key] = current_value
>>
>> KeyError: 'DEFAULT'
>
>> So what I'm trying to do on line 328 is to store the current key/value pair
>> in a dictionary under the proper section, which is in the proper set. Hence,
>> set[setname][section][key]=value
>>
>>
>>
>> You can see my insanely-verbose log here:
>>
>> http://python.pastebin.com/m6dcfb96d
>>
>>
>>
>> That was from a run where I commented out line 328… (that dos the actual
>>
>>
>>
>> Googling "KeyError" seems to indicate problems while trying to *retrieve*
>> values from a dictionary, whereas I'm trying to *set* values…
>
> Yes, KeyError happens on retrieval. Maybe you don't realize you are
> doing two retrievals, then a set:
>  set[current_set][current_section][current_key] = current_value
> means:
> - retrieve the value of set[current_set]
> - from the above value, retrieve the value associated with current_section
> - in the above value, set the value associated with current_key.
>
> I haven't looked at the code, but maybe current_set or current_section
> is 'DEFAULT
>

is 'DEFAULT' and that value hasn't been added yet. You have to
create/add the dicts at the top of the hierarchy before you can set
values at the lower part. Take a look at dict.setdefault() or
collections.defaultdict for some possible fixes.

Kent


More information about the Tutor mailing list