Integers class...?

Magnus Lie Hetland mlh at vier.idi.ntnu.no
Thu Mar 7 14:27:52 EST 2002


With all this talk about iteration etc. I just thought I'd chuck in
another idea. It is similar to several earlier ideas (and may even
have been proposed in exactly this form before -- I just haven't seen
it, or at least can't remember it).

One of the interval syntaxes (e.g. for for-loops) I'd like is
something like [0, 2 .. 10], similar to Haskell. This would require
syntax changes in Python, which is a bit unrealistic, but we already
have the three-dot Ellipsis, which is legal when indexing stuff.
Creating an Integers class with a standard-named instance 'ints', one
could do stuff like this (assuming for the moment that it's found in a
separate module):

  from ints import ints

  for x in ints[0, 2, ..., 10]:
      print x

The exact semantics (open/closed end etc.) would need to be decided
on, of course, but the implementation is trivial -- with iterators it
would be very simple to have infinite, lazy sequences too, like this:

  for x in ints[0, ...]:
      foobar(x)

Yes, this is just syntactic sugar (which for some might be "line
noise" -- a matter of taste, as always) for range(), but I think the
ellipsis notation is quite intutitive. Dropping the name in front of
the square brackets would be nice, of course, but that's less
realistic, I guess :)

Perhaps we could even make range indexable?

--
Magnus Lie Hetland                                  The Anygui Project
http://hetland.org                                  http://anygui.org



More information about the Python-list mailing list