[Python-ideas] Make bytes __repr__ and __str__ representation different?
Nick Timkovich
prometheus235 at gmail.com
Tue Nov 21 17:49:25 EST 2017
On Tue, Nov 21, 2017 at 11:22 AM, Chris Barker <chris.barker at noaa.gov>
wrote:
> 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 wonder if for repr-synonyms, a format specifier to `repr()` could toggle
how the object chooses to display itself would be handy:
x = b'*+-./'
repr(x) # b'*+-./'
repr(x, bytes.REPR_HEX_STRING) # b'\x2a\x2b\x2c\x2d\x2e'
repr(x, bytes.REPR_BYTES) # bytes([42, 43, 44, 45, 46])
repr(x, bytes.REPR_HEX_BYTES) # bytes([0x2A, 0x2B, 0x2C, 0x2D, 0x2E])
Kinda like `format()` but such that all of `eval(repr(x, <whatever>))` are
equal.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20171121/ca843dd2/attachment.html>
More information about the Python-ideas
mailing list