Message65316
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 long.
I have attached a patch, which follows the suggestion from Guido on how
to format the string so it is not confused with a list.
This is my first attempt at patching any part of the C code for Python.
Please let me know what should be changed and If I've missed something.
In particular I wonder whether I should be nesting any calls to PyNumber
functions or whether temporary variables should be used to avoid leaking
memory?
In addition I get the following warning on the line where I install the
range_str function in the PyRange_Type array.
Objects/rangeobject.c:357: warning: initialization from incompatible
pointer type
Brad |
|
Date |
User |
Action |
Args |
2008-04-10 20:18:29 | bmiller | set | spambayes_score: 0.0168754 -> 0.016875427 recipients:
+ bmiller |
2008-04-10 20:18:29 | bmiller | set | spambayes_score: 0.0168754 -> 0.0168754 messageid: <1207858708.98.0.537185652213.issue2610@psf.upfronthosting.co.za> |
2008-04-10 20:18:27 | bmiller | link | issue2610 messages |
2008-04-10 20:18:27 | bmiller | create | |
|