[Python-Dev] unicode hell/mixing str and unicode as dictionary keys

Ralf Schmitt ralf at brainbot.com
Fri Aug 4 11:18:28 CEST 2006


Jean-Paul Calderone wrote:
> 
> I like the exception that 2.5 raises.  I only wish it raised by default
> when using 'ascii' and u'ascii' as keys in the same dictionary. ;)  Oh,
> and that str and unicode did not hash like they do.  ;)

No problem:

 >>> import sys
 >>> reload(sys)
<module 'sys' (built-in)>
 >>> sys.setdefaultencoding("base64")
 >>> "a"==u"a"
Traceback (most recent call last):
   File "<stdin>", line 1, in <module>
   File "/exp/lib/python2.5/encodings/base64_codec.py", line 42, in 
base64_decode
     output = base64.decodestring(input)
   File "/exp/lib/python2.5/base64.py", line 321, in decodestring
     return binascii.a2b_base64(s)
binascii.Error: Incorrect padding
 >>> "a"=="a"
True
 >>> d={u"a":1, "a":1}
Traceback (most recent call last):
   File "<stdin>", line 1, in <module>
   File "/exp/lib/python2.5/encodings/base64_codec.py", line 42, in 
base64_decode
     output = base64.decodestring(input)
   File "/exp/lib/python2.5/base64.py", line 321, in decodestring
     return binascii.a2b_base64(s)
binascii.Error: Incorrect padding

Maybe this is all just a matter of choosing the right defaultencoding ? :)


BTW, python 2.4 also suppresses this exception (when instantiating the 
dictionary)

Does python 2.4 catch any exception when comparing keys (which are not 
basestrings) in dictionaries?


- Ralf



More information about the Python-Dev mailing list