comparing Unicode and string

luc.saffre at gmail.com luc.saffre at gmail.com
Thu Oct 19 20:04:55 CEST 2006


Thanks, John and Neil, for your explanations.

Still I find it rather difficult to explain to a Python beginner why
this error occurs.

Suggestion: shouldn't an error raise already when I try to assign s2? A
normal string should never be allowed to contain characters that are
not codable using the system encoding. This test could be made at
compile time and would render Python more didadic.

Luc

luc.saffre at gmail.com schrieb:

> Hello,
>
> here is something that surprises me.
>
>   #coding: iso-8859-1
>   s1=u"Frau Müller machte große Augen"
>   s2="Frau Müller machte große Augen"
>   if s1 == s2:
>       pass
>
> Running this code produces a UnicodeDecodeError:
>
> Traceback (most recent call last):
>   File "tmp.py", line 4, in ?
>     if s1 == s2:
> UnicodeDecodeError: 'ascii' codec can't decode byte 0xfc in position 6:
> ordinal not in range(128)
>
> I would have expected that "s1 == s2" gives True... or maybe False...
> but raising an error here is unnecessary. I guess that the comparison
> operator decides to convert s2 to a Unicode but forgets that I said
> #coding: iso-8859-1 at the beginning of the file.
> 
> TIA for any comments.
> 
> Luc Saffre




More information about the Python-list mailing list