[Python-ideas] Make bytes __repr__ and __str__ representation different?

Steven D'Aprano steve at pearwood.info
Tue Nov 21 10:16:23 EST 2017

On Tue, Nov 21, 2017 at 05:37:36PM +0300, Kirill Balunov wrote:
> Currently, __repr__ and __str__ representation of bytes is the same.
> Perhaps it is worth making them different, this will make it easier to
> visually perceive them as a container of integers from 0 to 255,
> instead of a mixture of printable and non-printable ascii characters. It is
> proposed:
> a) __str__ - leave unchanged
> b) __repr__ - represent as sequence of escaped hex
> >>> a = bytes([42,43,44,45,46])
> >>> a    # Current
> b'*+-./'
> >>> a    # Proposed
> b'\x2a\x2b\x2d\x2e\x2f'

I'd rather leave __str__ and __repr__ alone. Changing them will have 
huge backwards compatibility implications. I'd rather give bytes a 
hexdump() method that returns a string:

'2a 2b 2d 2e 2f'

(possibly with optional arguments to specify the formatting).

> As you can see, the second example is more easily perceived as a sequence,
> in which '\' is also perceived as ',' in list or tuple.

I disagree. And if you perceive \ as a separator, why does the sequence 
start with a separator? And why are there so many x characters?

> In addition, 2020
> is close, it allows the new Pythonistas not to take them as an ascii
> mixture strings.

The special role of ASCII is far too important for us to ever completely 
discard it.


More information about the Python-ideas mailing list