[Python-ideas] Make bytes __repr__ and __str__ representation different?
rosuav at gmail.com
Tue Nov 21 19:51:16 EST 2017
On Wed, Nov 22, 2017 at 9:49 AM, Nick Timkovich <prometheus235 at gmail.com> wrote:
> On Tue, Nov 21, 2017 at 11:22 AM, Chris Barker <chris.barker at noaa.gov>
>> 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
Methods are usually the best for that. Possibly with class methods to
perform the reconstruction - which in this case you have:
More information about the Python-ideas