On Wed, Nov 22, 2017 at 9:49 AM, Nick Timkovich firstname.lastname@example.org wrote:
On Tue, Nov 21, 2017 at 11:22 AM, Chris Barker email@example.com 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.
Methods are usually the best for that. Possibly with class methods to perform the reconstruction - which in this case you have: