PEP 276 Simple Iterator for ints
Chris Liechti
cliechti at gmx.net
Tue Nov 13 17:15:52 EST 2001
David Eppstein <eppstein at ics.uci.edu> wrote in
news:eppstein-CAD736.14225013112001 at news.service.uci.edu:
> As a possibly-related side note to this debate:
>
> I have been moving towards using Python-like pseudocode in my
> algorithms classes in place of the C/C++/Java-like pseudocode I was
> previously using; that is, using colons and indentation to mark blocks
> of code instead of curly braces, etc. As well as conciseness, Python
> offers some other advantages e.g. in the ability to write code with
> list comprehensions. The single biggest barrier for me to write actual
> working Python code is the range/xrange syntax: I just don't feel
> comfortable writing code like
>
> for i in range(n-1,-1,-1):
> for j in range(i+1,n+1):
> ...do something...
>
> because I don't expect my students (who are not required to learn
> Python) to understand from that syntax that the outer loop runs
> backwards from n-1 to 0 and that the inner loop runs from i+1 to n.
>
> Instead I have to make up some non-Python syntax which is less formal
> but immediately clear:
>
> for i in [n-1, n-2, ... 0]:
> for j in [i+1, i+2, ... n]:
> ...do something...
>
> (actual example from <http://www.ics.uci.edu/~eppstein/260/011023/>).
> As far as I can see, the PEP under discussion (which simply replaces
> the word "range" by "iter", creates an iterator instead of a list, and
> perhaps only allows the single-argument version of range) doesn't help
> me at all in my desire to have lecture notes which are both intuitively
> understandable and runnable.
python has callable instaces, which led me to the following experiment :-)
how a about something like that:
>>> class Int:
... def __call__(self, *args):
... return int(*args)
... def __getitem__(self, index):
... if isinstance(index, types.SliceType):
... if index.step:
... return range(index.start, index.stop, index.step)
... else:
... return range(index.start, index.stop)
... else:
... return range(index)
...
>>> myint = Int()
#and now for the user's view
#thats like the original:
>>> myint('123')
123
>>> myint('abc',16)
2748
#that's new:
>>> myint[1:7]
[1, 2, 3, 4, 5, 6]
>>>
>>> for i in myint[1:10]:
... print i,
...
1 2 3 4 5 6 7 8 9
>>>
for the "for" loop you have to write "myint[...]" and not just "[..]" as in
your example above, but you can think about selecting the number space for
the operation. (e.g. "mylong[...]" or "myfloat[...]" etc.)
of course the "range" should be replaced with an iterator, but i haven't
played around with them yet, so i took what i know that it works.
instead of "myint" this could be named "int" and override the builtin
function. this should not be a problem as it behaves like the original when
used like the original.
backwards compatibility is also no problem, since until now "int[]" raised
a "TypeError: unsubscriptable object".
for your student you could pack this into the site customization script.
i have to admit that this soloution doesn't yet looks that good when
counting backwards (like in your example above). but at least nobody asks
about what "range" is...
chris
--
Chris <cliechti at gmx.net>
More information about the Python-list
mailing list