<html><body style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space; ">After posting a patch to implement this some good discussion followed &nbsp;see: &nbsp;<a href="http://bugs.python.org/issue2610">http://bugs.python.org/issue2610</a><br><div><br>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.<br>Several new iterators have appeared in Python 3.0 that makes the language less transparent to beginning programmers. &nbsp;The examples that immediately come to mind are shown below, and I would guess there are others I haven't run across yet.<br><font class="Apple-style-span" face="'Bitstream Vera Sans Mono'"><br></font></div><div><font class="Apple-style-span" face="'Bitstream Vera Sans Mono'"><br></font></div><div><font class="Apple-style-span" face="'Bitstream Vera Sans Mono'">>>> range(10)</font><font class="Apple-style-span" face="'Bitstream Vera Sans Mono'"></font></div><div><font class="Apple-style-span" face="'Bitstream Vera Sans Mono'">range(0, 10)</font><font class="Apple-style-span" face="'Bitstream Vera Sans Mono'"></font></div><div><font class="Apple-style-span" face="'Bitstream Vera Sans Mono'">>>> myd = {chr(i):i for i in range(32,42)}</font><font class="Apple-style-span" face="'Bitstream Vera Sans Mono'"></font></div><div><font class="Apple-style-span" face="'Bitstream Vera Sans Mono'">>>> myd.keys()</font><font class="Apple-style-span" face="'Bitstream Vera Sans Mono'"></font></div><div><font class="Apple-style-span" face="'Bitstream Vera Sans Mono'">&lt;dict_keys object at 0xf31f0></font><font class="Apple-style-span" face="'Bitstream Vera Sans Mono'"></font></div><div><font class="Apple-style-span" face="'Bitstream Vera Sans Mono'">>>> myd.values()</font><font class="Apple-style-span" face="'Bitstream Vera Sans Mono'"></font></div><div><font class="Apple-style-span" face="'Bitstream Vera Sans Mono'">&lt;dict_values object at 0xf3220></font><font class="Apple-style-span" face="'Bitstream Vera Sans Mono'"></font></div><div><font class="Apple-style-span" face="'Bitstream Vera Sans Mono'">>>> myd.items()</font><font class="Apple-style-span" face="'Bitstream Vera Sans Mono'"></font></div><div><font class="Apple-style-span" face="'Bitstream Vera Sans Mono'">&lt;dict_items object at 0xf31f0></font></div><div><font class="Apple-style-span" face="'Bitstream Vera Sans Mono'"><br></font></div><div><br></div><div>Although none of the above are a problem for intermediate or advanced programmers &nbsp;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.<div><br></div><div>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. &nbsp;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. &nbsp;For example it already handles a return value of None in a special way, maybe it should do something for these iterators as well.</div><div><br></div><div>Any other comments or ideas?</div><div><br></div><div>Thanks,</div><div><br></div><div>Brad</div><div><br></div></div><div><html>On Apr 7, 2008, at 6:24 PM, Guido van Rossum wrote:</html><br class="Apple-interchange-newline"><blockquote type="cite"><div>I'd object to it returning something that resembles a list too<br>closely, but I could live with str(range(3)) return &lt;0, 1, 2>. We<br>should probably have a cutoff so that if there are more than 6 values<br>it'll show the first 3 values, then dots, then the last 2 values. (The<br>cutoff would be computed so that '...' always represents at least 2<br>values.<br><br>On Mon, Apr 7, 2008 at 4:14 PM, Brad Miller &lt;<a href="mailto:bonelake@gmail.com">bonelake@gmail.com</a>> wrote:<br><blockquote type="cite">Hi,<br></blockquote><blockquote type="cite"><br></blockquote><blockquote type="cite"> I use Python in my CS1 and CS2 curriculum and I have a question.<br></blockquote><blockquote type="cite"> As I've been using the Python 3.0 alphas one of the things that I am<br></blockquote><blockquote type="cite"> bothered by is that I cannot see the sequence produced by range<br></blockquote><blockquote type="cite"> without introducing students to the list() function.<br></blockquote><blockquote type="cite"><br></blockquote><blockquote type="cite"> I typically introduce range on day 1 of class and show students what<br></blockquote><blockquote type="cite"> it produces without making a big deal out of the fact that it creates<br></blockquote><blockquote type="cite"> a list. &nbsp;They all accept this and things work out nicely when I<br></blockquote><blockquote type="cite"> introduce lists for real in a week or two.<br></blockquote><blockquote type="cite"><br></blockquote><blockquote type="cite"> My question is why couldn't the __str__ method for the range object be<br></blockquote><blockquote type="cite"> more friendly and show a representation of the sequence? &nbsp;I understand<br></blockquote><blockquote type="cite"> why __repr__ should return range(0,10) for an object created using<br></blockquote><blockquote type="cite"> range(10) but couldn't print(range(10)) produce [0, 1, 2, ... 9]<br></blockquote><blockquote type="cite"> The ... could even be used if the sequence were excessively wrong.<br></blockquote><blockquote type="cite"><br></blockquote><blockquote type="cite"> If this is acceptable, I would be happy to accept the challenge of<br></blockquote><blockquote type="cite"> providing a patch.<br></blockquote><blockquote type="cite"><br></blockquote><blockquote type="cite"> Thanks,<br></blockquote><blockquote type="cite"><br></blockquote><blockquote type="cite"> Brad<br></blockquote><blockquote type="cite"><br></blockquote><blockquote type="cite"><br></blockquote><blockquote type="cite"> _______________________________________________<br></blockquote><blockquote type="cite"> Python-Dev mailing list<br></blockquote><blockquote type="cite"> <a href="mailto:Python-Dev@python.org">Python-Dev@python.org</a><br></blockquote><blockquote type="cite"> <a href="http://mail.python.org/mailman/listinfo/python-dev">http://mail.python.org/mailman/listinfo/python-dev</a><br></blockquote><blockquote type="cite"> Unsubscribe: <a href="http://mail.python.org/mailman/options/python-dev/guido%40python.org">http://mail.python.org/mailman/options/python-dev/guido%40python.org</a><br></blockquote><blockquote type="cite"><br></blockquote><br><br><br>-- <br>--Guido van Rossum (home page: <a href="http://www.python.org/~guido/)">http://www.python.org/~guido/)</a><br></div></blockquote></div><br></body></html>