[Python-Dev] string representation of range in 3.0

Steven D'Aprano steve at pearwood.info
Wed Apr 16 01:40:39 CEST 2008

Hi folks,

Been lurking for a while, this is my first post.

As I recall, this discussion was started because of concern that Python 
3 had introduced features that made it less friendly to beginners. If I 
can quote Brad Miller:

"Several new iterators have appeared in Python 3.0 that makes the  
language less transparent to beginning programmers.  The examples that  
immediately come to mind are shown below..."

It hasn't been that many years since I was a beginner myself, and I'm 
pretty sure that had I seen Guido's suggestion:

> That's why I proposed <0, 1, ..., 9> for repr(range(10)). (And I
> meant the '...' literally, i.e. if there are more than 4 values,
> replace all but the first two and the last with three dots. And yes,
> I mean that str(range(4)) == '<0, 1, 2, 3>' but str(range(5)) == '<0,
> 1, ..., 4>'.

in the interpreter back then, I would have tried writing the literal <0, 
1, 2, 3> as an alternative to [0, 1, 2, 3] or (0, 1, 2, 3) and been 
rather distressed that it didn't do what I expected. As a beginner I 
was rather hazy on the difference between lists and tuples, and I would 
have imagined that <> was just another sort of delimiter pair for a 
list-like thing.

I'd like to say that I'd only make the mistake once, but thinking about 
how often I still write dict(key) instead of dict[key], it's quite 
possible that some people might take a long time to drive the <> 
pseudo-delimiters out of their head.

I'm -1 on <>. In my experience, real beginners aren't going to using 
range() except in the context of for loops, hence are unlikely to be 
printing the range object directly except by accident.

Personally, I find it a feature that this happens in Python 2.x:

>>> xrange(5)

and would say the same thing about range() in Python 3, regardless of 
whether you wrap it in a str() or a repr().

As for the use of dots to keep stringified-sequences short, I'd vote +1 
except I think that four items is rather short and would prefer the 
dots to come in around six or eight items. That's not entirely an 
aesthetic preference, I also have a reason for thinking that it might 
actually make a difference, but to avoid turning this discussion into 
bike-shedding I'll keep it to myself unless asked.


More information about the Python-Dev mailing list