[Python-ideas] RFC: bytestring as a str representation [was: a new bytestring type?]

Guido van Rossum guido at python.org
Tue Jan 7 22:52:41 CET 2014


On Tue, Jan 7, 2014 at 10:58 AM, Ethan Furman <ethan at stoneleaf.us> wrote:
> On 01/07/2014 12:49 PM, Guido van Rossum wrote:
>>
>> On Tue, Jan 7, 2014 at 9:43 AM, Ethan Furman <ethan at stoneleaf.us> wrote:
>>>
>>> My vision for a bytestring type (more refined):
>>>
>>>    - made up of single bytes in the range 0 - 255 (no unicode anywhere)
>>>
>>>    - indexing returns a bytestring of length 1, not an integer (as bytes
>>> does)
>>>
>>>    - `bytestring(7)` either fails, or returns 'bytestring('\x07')' not
>>> 'bytestring(0, 0, 0, 0, 0, 0, 0)'
>>
>>
>> It sounds like you are just unhappy with some of the behavior of the
>> bytes object. I agree that these two behaviors are suboptimal, but it
>> is just too late to change them, and it's not enough to add a new type
>> -- not by a long shot. The constructor behavior can be changed using a
>> custom factory function. The indexing behavior, unfortunately, needs
>> to be dealt with by changing b[i] into b[i:i+1] everywhere.

> Of course I'm unhappy with it, it doesn't behave the way I think it should,
> and it's not consistent.

Consistent with what? (Before you rush in an answer, remember that
there are almost always multiple sides to a consistency argument.)

> The reason I started the thread was to hopefully gather others requirements
> to have a truly distinct and useful new type.  Doesn't seem to have
> happened, though.  :(

So now is the time to man up and live with it. It's not going to change.

> Is it too late to change the repr for bytes?

Yes.

> I can't think of anywhere else
> in the stdlib where what you see is not what you get:
>
> --> [0, 1, 2]
> [0, 1, 2]
>
> --> [0, 1, 2][1]
> 1
>
> --> {'this':'that', 'these':'those'}
> {'this': 'that', 'these': 'those'}
>
> --> {'this':'that', 'these':'those'}['these']
> 'those'
>
> --> 'abcdef'
> 'abcdef'
>
> --> 'abcdef'[3]
> 'd'
>
> But of course with bytes:
>
> --> b'abcdef'
> b'abcdef'
>
> --> b'abcdef'[3]
> 100

I don't see what's wrong with those. Both produce valid expressions
that, when entered, compare equal to the object whose repr() was
printed. What more would you *want*?

-- 
--Guido van Rossum (python.org/~guido)


More information about the Python-ideas mailing list