[issue35200] Range repr could be better

Julien Palard report at bugs.python.org
Sun Nov 11 19:28:44 EST 2018


Julien Palard <julien+python at palard.fr> added the comment:

I understand we like round-tripping represnetations, I think we like them because in much cases it's immediatly and unambiguously understandable by a Python developer.

It's the best representation as it's the one conveying the most information. But `range(0, 10)` convery very few information, one may forget the "begin included, end excluded" rule (or even if the 2nd one is the end of the length). I think the following is more usefull:

>>> range(10)
<range object [0, 1, ..., 8, 9]>
>>> range(10, 2)
<range object []>
>>> range(2, 10)
<range object [2, 3, ..., 8, 9]>
>>> range(2, 10, 2)
<range object [2, 4, 6, 8]>
>>> 



@steven:

I dont think moving this to __str__ would help someone: I've never seen any student try `str(range(10))` in the repl, they all naturally try the bare `range(10)` and they're all presented with un-informative information. If someone is here to teach them to try with str, better try with list(range(10)) or *range(10).

As for repr(range(0)) == repr(range(2, 2)) == repr(range(1, 5, -1)) I do not consider this a bug, they are all strictly equivalent as being the empty range (when speaking of a mathematical object, maybe not the in-memory struct).


@raymond:

I'm also not OK to teach `*repr(10)` during the first class. I personally go for `list(range(10))`, but I can only because I'm physically available when they ask why the information displayed is not what they expect. A lot of people are learning Python at home and they're probably just lost while being presented with the round-tripping representation.


I don't really agree that changing the repr could break code doing `eval(repr(range(10)))`, is it really something people do?


@nick:

I agree changing the repr could break some doctests on function returning ranges, on the other hand I've never seen a function returning a range.

----------

_______________________________________
Python tracker <report at bugs.python.org>
<https://bugs.python.org/issue35200>
_______________________________________


More information about the Python-bugs-list mailing list