<float>range(0.0,100.0,0.1)

erik_wilsher at my-deja.com erik_wilsher at my-deja.com
Thu May 18 02:21:24 EDT 2000


In article <DUCU4.21867$HG1.541688 at nnrp1.uunet.ca>,
  "Warren Postma" <embed at geocities.com> wrote:
> how do you do this:
>
> for i in range(0.0, 100.0, 0.1):
>     print i
>
you can make a frange object approximately like this:

(I did this a year ago, but can't find the code again, so this is
untested)

class frange:
    def __init__(self, min, max=None, step=None):
        if max == None:
            self.min = 0.
            self.max = min
        else:
            self.min = min
            self.max = max

        if step == 0.0:
            raise ValueError, "zero step for frange()"
        elif step == None:
            self.step = 1.0
        else:
            self.step = step

    def __getitem__(self,item):
        rv = self.min + self.step*item
        if rv >= self.max:   #not sure about the boundry here
            raise IndexError, "list index out of range"
        return rv

(rought code, no cheching for max < min, step < 0.0 etc)

The main point is that the iteration in python is terminated through an
IndexError exception.  GvR has indicated that the iteration control
will change in py3k.

      Erik Wilsher



Sent via Deja.com http://www.deja.com/
Before you buy.



More information about the Python-list mailing list