[Python-Dev] string representation of range in 3.0

Brad Miller bonelake at gmail.com
Mon Apr 14 21:45:14 CEST 2008


After posting a patch to implement this some good discussion followed   
see:  http://bugs.python.org/issue2610

It was suggested that a broader discussion might be in order around  
the issue of iterators and how they are displayed in the command line  
interpreter.
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, and I would guess there are  
others I haven't run across yet.


 >>> range(10)
range(0, 10)
 >>> myd = {chr(i):i for i in range(32,42)}
 >>> myd.keys()
<dict_keys object at 0xf31f0>
 >>> myd.values()
<dict_values object at 0xf3220>
 >>> myd.items()
<dict_items object at 0xf31f0>


Although none of the above are a problem for intermediate or advanced  
programmers  I would like to find a way so that beginning students  
would automatically get a more helpful representation when they  
evaluate expressions in the interpreter.

My solution of implementing the __str__ method for range is one  
solution, and that could also be done for the dict_xxx objects as  
well.  Other solutions that were suggested were to include some kind  
of a module that overrides sys.displayhook or to simply make the  
command line interpreter more intelligence.  For example it already  
handles a return value of None in a special way, maybe it should do  
something for these iterators as well.

Any other comments or ideas?

Thanks,

Brad

On Apr 7, 2008, at 6:24 PM, Guido van Rossum wrote:

> I'd object to it returning something that resembles a list too
> closely, but I could live with str(range(3)) return <0, 1, 2>. We
> should probably have a cutoff so that if there are more than 6 values
> it'll show the first 3 values, then dots, then the last 2 values. (The
> cutoff would be computed so that '...' always represents at least 2
> values.
>
> On Mon, Apr 7, 2008 at 4:14 PM, Brad Miller <bonelake at gmail.com>  
> wrote:
>> Hi,
>>
>> I use Python in my CS1 and CS2 curriculum and I have a question.
>> As I've been using the Python 3.0 alphas one of the things that I am
>> bothered by is that I cannot see the sequence produced by range
>> without introducing students to the list() function.
>>
>> I typically introduce range on day 1 of class and show students what
>> it produces without making a big deal out of the fact that it creates
>> a list.  They all accept this and things work out nicely when I
>> introduce lists for real in a week or two.
>>
>> My question is why couldn't the __str__ method for the range object  
>> be
>> more friendly and show a representation of the sequence?  I  
>> understand
>> why __repr__ should return range(0,10) for an object created using
>> range(10) but couldn't print(range(10)) produce [0, 1, 2, ... 9]
>> The ... could even be used if the sequence were excessively wrong.
>>
>> If this is acceptable, I would be happy to accept the challenge of
>> providing a patch.
>>
>> Thanks,
>>
>> Brad
>>
>>
>> _______________________________________________
>> Python-Dev mailing list
>> Python-Dev at python.org
>> http://mail.python.org/mailman/listinfo/python-dev
>> Unsubscribe: http://mail.python.org/mailman/options/python-dev/guido%40python.org
>>
>
>
>
> -- 
> --Guido van Rossum (home page: http://www.python.org/~guido/)

-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.python.org/pipermail/python-dev/attachments/20080414/be28e179/attachment.htm 


More information about the Python-Dev mailing list