[Python-ideas] Fixing the Python 3 bytes constructor
Nick Coghlan
ncoghlan at gmail.com
Sat Mar 29 22:34:44 CET 2014
On 30 March 2014 07:09, Ethan Furman <ethan at stoneleaf.us> wrote:
> On 03/29/2014 01:59 PM, Alexander Heger wrote:
>>>
>>>
>>> I'm also +1 on Benjamin's idea of the iterate-as-bytes addition. That
>>> just
>>> leaves the inability to index into bytes and get back a single as the
>>> biggest annoyance in the bytes API.
>>
>>
>> The issue remains that there is no single byte type, but what is
>> returned is bytes type over which, again, you can iterate. This is
>> different from a tuple of integers, when you iterate over that, you
>> get integers not length-one tuples of integers. The issue, I think,
>> originates from the string concept where iteration also return a
>> string not a character - which python does not have. A logical
>> extension would be to have to have character and byte types that are
>> returned when iterating of strings and bytes. This is like taking a
>> giraffe out of a zoo and still calling the result a (small) zoo. Or
>> calling a can of tuna a grocery store.
>
>
> Nice examples. The problem is that one *can* say:
>
> b'a'
>
> and get a 'bytes' object of length one, and then quite naturally one would
> then try something like
>
> b'abc'[0] == b'a'
>
> and that will fail, even though it *looks* correct.
FWIW, this is part of why Python 3 *didn't* originally have a bytes
literal - the affordances of the str-like literal syntax are wrong for
a tuple-of-integers type. We only added it back (pre 3.0) because
people wanted it for working with wire protocols that contain ASCII
segments (and I think that was a reasonable decision, even though it
has contributed directly to people several years later still expecting
them to behave more like Python 2 strings)
Cheers,
Nick.
--
Nick Coghlan | ncoghlan at gmail.com | Brisbane, Australia
More information about the Python-ideas
mailing list