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

Kirill Balunov kirillbalunov at gmail.com
Tue Nov 21 09:37:36 EST 2017


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'

As you can see, the second example is more easily perceived as a sequence,
in which '\' is also perceived as ',' in list or tuple. In addition, 2020
is close, it allows the new Pythonistas not to take them as an ascii
mixture strings.

With kind regards, -gdg
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20171121/4a8ad8a5/attachment.html>


More information about the Python-ideas mailing list