Movatterモバイル変換
[0]ホーム
[Python-Dev] Re: Unicode and comparisons
M.-A. Lemburgmal@lemburg.com
Tue, 04 Apr 2000 18:47:51 +0200
"Martin v. Loewis" wrote:>> > Question: is this behaviour acceptable or should I go even further> > and mask decoding errors during compares and contains tests too ?>> I always thought it is a core property of cmp that it works between> all objects.It does, but not necessarily without exceptions. I could easilymask the decoding errors too and then have cmp() work exactlyas for strings, but the outcome may be different to what theuser had expected due to the failing conversion. Sorting ordermay then look quite unsorted...> Because of that,>> >>> x=[u'1','aäöü']> >>> x.sort()> Traceback (most recent call last):> File "<stdin>", line 1, in ?> UnicodeError: UTF-8 decoding error: invalid data>> fails. As always in cmp, I'd expect to get a consistent outcome here> (ie. cmp should give a total order on objects).>> OTOH, I'm not so sure why cmp between plain and unicode strings needs> to perform UTF-8 conversion? IOW, why is it desirable that>> >>> 'a' == u'a'> 1This is needed to enhance inter-operability between Unicodeand normal strings. Note that they also have the same hashvalue (provided both use the ASCII code range), making theminterchangeable in dictionaries:>>> d={u'a':1}>>> d['a'] = 2>>> d[u'a']2>>> d['a']2This is per design. > Anyway, I'm not objecting to that outcome - I only think that, to get> cmp consistent, it may be necessary to drop this result. If it is not> necessary, the better.-- Marc-Andre Lemburg______________________________________________________________________Business:http://www.lemburg.com/Python Pages:http://www.lemburg.com/python/
[8]ページ先頭