[Python-Dev] Dicts are broken Was: unicode hell/mixing str and unicode asdictionarykeys

Paul Colomiets pc at gafol.net
Fri Aug 4 22:32:42 CEST 2006


Giovanni Bajo wrote:
> Paul Colomiets <pc at gafol.net> wrote:
>
>   
>> Well it's not recomended to mix strings and unicode in the
>> dictionaries
>> but if we mix for example integer and float we have the same thing. It
>> doesn't raise exception but still it is not expected behavior for me:
>>  >>> d = { 1.0: 10, 2.0: 20 }
>> then if i somewhere later do:
>>  >>> d[1] = 100
>>  >>> d[2] = 200
>> to have here all floats in d.keys(). May be this is not a best
>> example.
>>     
>
> There is a strong difference. Python is moving towards unifying number types in
> a way (see the true division issue): the idea is that, all in all, user
> shouldn't really care what type a number is, as long as he knows it's a number.
> On the other hand, unicode and str are going to diverge more and more.
>
> Giovanni Bajo
>
>   
It makes sense, but consider this example:

 >>> from decimal import Decimal
 >>> d = {}
 >>> d[Decimal(0)] = 1
 >>> d[0] = 2
 >>> d[Decimal("0.5")] = 3
 >>> d[0.5]  = 4
 >>> d.keys()
[Decimal("0"), 0.5, Decimal("0.5")]

I expect d.keys() to have 2 or 4 keys but don't 3, it's confusing. Don't 
you think that someday line "d[0.5] = 4" will raise exception? Or at 
least that it should raise if mixing str and unicode raises?

--
Regards,
  Paul.


More information about the Python-Dev mailing list