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


Groups > comp.lang.python > #81098

Re: python 2.7 and unicode (one more time)

From random832@fastmail.us
Subject Re: python 2.7 and unicode (one more time)
Date 2014-11-20 12:42 -0500
References <m4kcu8$ta8$1@ger.gmane.org> <m4kn66$ma3$1@ger.gmane.org> <1416491999.209592.193345213.3A6FF9A4@webmail.messagingengine.com> <CAPTjJmr5NWMFCfU8jaNd2g2f9cQ1y+zO3qT7yVcC_m9vKXN6fw@mail.gmail.com>
Newsgroups comp.lang.python
Message-ID <mailman.16035.1416505382.18130.python-list@python.org> (permalink)

Show all headers | View raw


On Thu, Nov 20, 2014, at 09:59, Chris Angelico wrote:
> 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?

Because a bytes format string suggests a bytes result. Why does unicode
always "win", rather than the type of the format string always winning?

> Makes much better sense to work in
> Unicode. But mainly, it has to do one of them, and be predictable.

Yeah, but string % is not a symmetrical operator. People's mental model
of it is likely to be that it acts like format (which does use the type
of the format string) or C sprintf/wsprintf (both of which use the same
type for the format string and result). And literally every other type
is converted to the type of the format string when used with %s - having
unicode be special adds cognitive load, and it means you can't safely
blindly use %s with an unknown object.

Back to comp.lang.python | Previous | Next | Find similar | Unroll thread


Thread

Re: python 2.7 and unicode (one more time) random832@fastmail.us - 2014-11-20 12:42 -0500

csiph-web