470,648 Members | 1,608 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 470,648 developers. It's quick & easy.

Working xrange() replacement proposal

You guys are probably sick of these by now, but I've come up with an
xrange() replacement that is actually (almost) implementable in pure Python.

Some have previously suggested the form "for 0<=i<10:" to indicate iterating
over a range of integers. This form works well in languages like Icon that
can bind variables to all possible values, but Python doesn't (yet?) have
this ability. Instead, I propose the syntax "for i in 0<=ints<10:". Though
slightly more verbose, this form has the advantage of being more "Pythonic"
in nature, binding i to each integer in the given range in turn.

The other (major) advantage of this form is that it is implementable in pure
Python: 'ints' is simply an object which represents the mathematical set of
all integers. Its comparison functions return a similar object representing
a portion of this set, and its __iter__ method returns an iterator over
these integers.

My implementation defines an 'intrange' class to represent these ranges,
along with (previously proposed) Min and Max objects to represent the
minimum and maximum integers. The intrange constructor accepts two values, a
lower and upper bound (with the same meaning as those of xrange()).

Two default intrange objects are defined, 'ints' and 'nats'. ints has
upper and lower bounds of Max and Min, respectively, whereas nats has an
upper bound of Max and a lower bound of 0.

Limitations of this implementation:

* Python doesn't allow overriding of the 'and' operator. Since chained
comparisons are implemented using this operator, "for i in 0<=ints<10:"
won't work (it will instead iterate over all integers less than 10). Two
workarounds are to use either "(0<=ints)<10" or "(0<=ints)&(ints<10)".
Ideally intrange will be able to override "and" the same way it does "&".

* 'Broken' ranges aren't supported; i.e. the ranges can't have any gaps in
them. Whether or not this would be actually be useful (e.g. to emulate
xrange(0,10,2)) is a question yet to be answered.

The implementation is available at
http://users.wpi.edu/~squirrel/temp/ints.py. Test it (in 2.3) with:

from ints import ints,nats
for i in (5<=ints)<10: print i
print list(nats<10)
Jul 18 '05 #1
0 936

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

reply views Thread by Christopher T King | last post: by
6 posts views Thread by Gerrit Holl | last post: by
7 posts views Thread by Christian Neumann | last post: by
reply views Thread by Christian Neumann | last post: by
92 posts views Thread by Reed L. O'Brien | last post: by
30 posts views Thread by Mike Meyer | last post: by
35 posts views Thread by bonono | last post: by
29 posts views Thread by Steve R. Hastings | last post: by
3 posts views Thread by Bas | last post: by
1 post views Thread by Korara | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.