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

Chris Barker chris.barker at noaa.gov
Tue Nov 21 12:22:14 EST 2017


On Tue, Nov 21, 2017 at 6:37 AM, Kirill Balunov <kirillbalunov at gmail.com>
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'
>

supposedly __repr__ is supposed to give an eval-able version -- which your
proposal is. But the way you did your example indicates that:

bytes((42, 43, 44, 45, 46))

would be an even better __repr__, if the goal is to make it clear and easy
that it is a "container of integers from 0 to 255"

I've been programming since quite some time ago, and hex has NEVER come
naturally to me :-)

But backward compatibility and all that :-(

-CHB



-- 

Christopher Barker, Ph.D.
Oceanographer

Emergency Response Division
NOAA/NOS/OR&R            (206) 526-6959   voice
7600 Sand Point Way NE   (206) 526-6329   fax
Seattle, WA  98115       (206) 526-6317   main reception

Chris.Barker at noaa.gov
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20171121/137f7f37/attachment.html>


More information about the Python-ideas mailing list