Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #81078 > unrolled thread

Re: python 2.7 and unicode (one more time)

Started byChris Angelico <rosuav@gmail.com>
First post2014-11-21 01:59 +1100
Last post2014-11-21 01:59 +1100
Articles 1 — 1 participant

Back to article view | Back to comp.lang.python

This discussion starts older than the indexed window; earlier articles aren't shown. The article labeled Started by below is the oldest one visible, not the original post.


Contents

  Re: python 2.7 and unicode (one more time) Chris Angelico <rosuav@gmail.com> - 2014-11-21 01:59 +1100

#81078 — Re: python 2.7 and unicode (one more time)

FromChris Angelico <rosuav@gmail.com>
Date2014-11-21 01:59 +1100
SubjectRe: python 2.7 and unicode (one more time)
Message-ID<mailman.16020.1416495561.18130.python-list@python.org>
On Fri, Nov 21, 2014 at 12:59 AM,  <random832@fastmail.us> wrote:
> On Thu, Nov 20, 2014, at 07:35, Peter Otten wrote:
>> >>> "%s nötig %s" % (u"üblich", u"ähnlich")
>> Traceback (most recent call last):
>>   File "<stdin>", line 1, in <module>
>> UnicodeDecodeError: 'ascii' codec can't decode byte 0xc3 in position 4:
>> ordinal not in range(128)
>
> This is surprising to me - why is it trying to decode the format string,
> rather than encode the arguments?

Why should it encode to bytes? Makes much better sense to work in
Unicode. But mainly, it has to do one of them, and be predictable. If
you add a float and an int, you have to predictably get back one of
those two types, and since neither is a perfect superset of the other,
one just has to be picked. (And that's float, because it's more likely
to be the better option.) In this case, picking Unicode to meet on is
easily the better option, because you'll often have pure-ASCII string
literals as format strings, and Unicode data being interpolated into
it. So using an ASCII codec is far more likely to succeed if you
decode the format string than if you encode the data.

Personally, I'd much rather be very clear about what's text and what's
bytes, and not have any automatic encoding at all. That's why I use
Python 3.

ChrisA

[toc] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web