Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I believe there is no backport (Py 3's completely removed the int/long distinction, after all, but in 2.* it's here to stay;-) but it's not hard to hack your own, e.g....:</p> <pre><code>import operator def wowrange(start, stop, step=1): if step == 0: raise ValueError('step must be != 0') elif step &lt; 0: proceed = operator.gt else: proceed = operator.lt while proceed(start, stop): yield start start += step </code></pre> <p><strong>Edit</strong> it appears the OP doesn't just want looping (the normal purpose of xrange, and range in Py3), but also <code>len</code> and the <code>in</code> operator (the latter does work on the above generator, but slowly -- optimizations are possible). For such richness a class is better...:</p> <pre><code>import operator class wowrange(object): def __init__(self, start, stop=None, step=1): if step == 0: raise ValueError('step must be != 0') if stop is None: start, stop = 0, start if step &lt; 0: self.proceed = operator.gt self.l = (stop-start+step+1)//step else: self.proceed = operator.lt self.l = (stop-start+step-1)//step self.lo = min(start, stop) self.start, self.stop, self.step = start, stop, step def __iter__(self): start = self.start while self.proceed(start, self.stop): yield start start += self.step def __len__(self): return self.l def __contains__(self, x): if x == self.stop: return False if self.proceed(x, self.start): return False if self.proceed(self.stop, x): return False return (x-self.lo) % self.step == 0 </code></pre> <p>I wouldn't be surprised if there's an off-by-one or similar glitch lurking here, but, I hope this helps!</p> <p><strong>Edit</strong> again: I see indexing is ALSO required. Is it just too hard to write your own <code>__getitem__</code>? I guess it is, so here it, too, is, served on a silver plate...:</p> <pre><code> def __getitem__(self, i): if i &lt; 0: i += self.l if i &lt; 0: raise IndexError elif if i &gt;= self.l: raise IndexError return self.start + i * self.step </code></pre> <p>I don't know if 3.0 <code>range</code> supports slicing (<code>xrange</code> in recent <code>2.*</code> releases doesn't -- it used to, but that was removed because the complication was ridiculous and prone to bugs), but I guess I do have to draw a line in the sand somewhere, so I'm not going to add it;-).</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload