Cult-like behaviour [was Re: Kindness]

Chris Angelico rosuav at gmail.com
Mon Jul 16 17:57:04 EDT 2018


On Tue, Jul 17, 2018 at 7:50 AM, Roel Schroeven <roel at roelschroeven.net> wrote:
> Steven D'Aprano schreef op 16/07/2018 2:18:
>>
>> On Sun, 15 Jul 2018 16:08:15 -0700, Jim Lee wrote:
>>
>>> Python3 is intrinsically tied to Unicode for string handling. Therefore,
>>> the Python programmer is forced to deal with it (in all but trivial
>>> cases), rather than given a choice.  So I don't understand how I can
>>> illustrate my point with Python code since Python won't let me deal with
>>> strings without also dealing with Unicode.
>>
>>
>> Nonsense.
>>
>> b"Look ma, a Python 2 style ASCII string."
>
>
> Except for one little difference, which has bitten be me a few times.
> Consider this code:
>
> from __future__ import print_function
> s = b"Look ma, a Python 2 style ASCII string."
> print('First element:', s[0])
>
> Result in Python 2: First element: L
> Result in Python 3: First element: 76
>
> Likewise this code:
>
> from __future__ import print_function
> for e in b'hello':
>   print(e, end=', ')
> print()
>
> Result in Python 2: h, e, l, l, o,
> Result in Python 3: 104, 101, 108, 108, 111,
>
> There are times (encoding/decoding network protocols and other data formats)
> when I have a byte string and I want/need to process it like Python 2 does,
> and that is the one area where I feel Python 3 make things a bit more
> difficult.
>

For the most part, you probably want to decode it as ASCII, if you
want to process it as text. Remember, bytes are simply numbers -
octets, groups of eight bits. For it to mean the English word "hello",
that byte sequence has to be interpreted as ASCII, which is accurately
indicated as b'hello'.decode('ascii').

ChrisA


More information about the Python-list mailing list