Why KeyError ???

Paul Rubin phr-n2002a at nightsong.com
Tue Mar 5 22:22:01 EST 2002


"Raymond Hettinger" <python at rcn.com> writes:
> > KeyError: šđčćž
> > >>> a == b
> > 1
> 
> Hmm.  I don't get the same identity check results as you do:
> >>> a = '\xe7\xd0\x9f\x86\xa7'
> >>> b = unicode(a,'cp1250')
> >>> a is b
> 0

Try == rather than 'is'.  The docs are a little bit imprecise about
what's supposed to happen here, but 2.2.7 "Mapping types" says about
numeric keys:

    A dictionary's keys are almost arbitrary values. The only types of
    values not acceptable as keys are values containing lists or
    dictionaries or other mutable types that are compared by value rather
    than by object identity. Numeric types used for keys obey the normal
    rules for numeric comparison: if two numbers compare equal (e.g. 1 and
    1.0) then they can be used interchangeably to index the same
    dictionary entry.

That makes it surprising if two unicode strings that compare as ==
don't index the same dictionary item.



More information about the Python-list mailing list