[Python-ideas] Float range class

Todd toddrjen at gmail.com
Thu Jan 8 17:01:03 CET 2015

Currently, the range class is limited to integers.  However, in many
applications, especially science and mathematics, ranges of float values
are the norm.

The current approaches for float ranges all have significant limitations.
numpy.arange pre-allocates the values and doesn't use python floats, both
of which affect performance.  List comprehensions also have to be
pre-allocated.  Generators and values created in the body of a loop cannot
be indexed or re-used and are much more verbose and harder to read.  So it
would be nice to have a float equivalent of the existing range class.

Assuming such a class is worthwhile, I see three possible ways to handle
float ranges, each with its own strengths and weaknesses.  I am not sure
which, if any, is preferable.

The first is to extend the existing range class so it can support floats or
to use duck-typing to support any class that has the necessary methods.
However, I would be worried about making such a fundamental change to such
a core class, and either approach could hurt performance.

The second approach is to create a new class that handles floating-point
ranges (perhaps called "frange") and/or a new class that handles duck-typed
ranges (perhaps called "drange").  However, there isn't really a good place
right now to put these as far as I can see.

The third approach is to create a new module for various range classes.
For example there could be a float range, a datetime range, a decimal
range, a duck-typed range, an abstract base class for making new range
classes, etc.  However, this is a lot more work (although it wouldn't need
to all be done at one time).
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20150108/3c93291f/attachment.html>

More information about the Python-ideas mailing list