[Python-ideas] Float range class

Nathaniel Smith njs at pobox.com
Thu Jan 8 19:40:12 CET 2015


On Thu, Jan 8, 2015 at 5:58 PM, Todd <toddrjen at gmail.com> wrote:
> On Thu, Jan 8, 2015 at 6:41 PM, Andrew Barnert <abarnert at yahoo.com> wrote:
>>
>> More generally: if an frange that works exactly like range but with floats
>> (and rounding errors) is all you want, that's so trivial to write that it
>> doesn't seem worth putting in the stdlib, unless the use case is very
>> common.
>
> In numerical computing the use-case is extremely common, in fact I would say
> it practically universal.  Ideally it would be vectorized, but in practice
> this is often not possible, requiring the use of loops.

Can you give a more specific example? I believe your problem is real,
but as a numerical programmer I can't think of an example of where
I've ever wanted anything like this, so it's hard for me to visualize
what exactly you're talking about.

And is what you want just:

class FRange:
    def __init__(start, stop, step):
        self.start = start
        self.stop = stop
        self.step = step

    def __getitem__(self, i):
        x = self.start + i * self.step
        if x > self.stop:
            raise IndexError

    def __iter__(self):
        i = 0
        while True:
            try:
                yield self[i]
            except IndexError:
                return
            i += 1

?

-n

-- 
Nathaniel J. Smith
Postdoctoral researcher - Informatics - University of Edinburgh
http://vorpus.org


More information about the Python-ideas mailing list