Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
What is xrange?
#7
It's just like range(), except it only generates numbers as you ask for them (instead of all at once).  If you wanted to see what it looks like in practice, here's a bad version I just made up:

>>> class xrange:
...   def __init__(self, min=0, max=None, step=1):
...     if max is None:
...       max = min
...       min = 0
...     self.max = max
...     self.step = step
...     self.current = min
...   def __iter__(self):
...     return self
...   def __next__(self):
...     value = self.current
...     self.current += self.step
...     if self.step > 0:
...       if value >= self.max:
...         raise StopIteration()
...     else:
...       if value <= self.max:
...         raise StopIteration()
...     return value
...
>>> for i in xrange(5):
...   print(i)
...
0
1
2
3
4
>>> for i in xrange(2, 12, 3):
...   print(i)
...
2
5
8
11
Reply


Messages In This Thread
What is xrange? - by failedSIGNAL - May-30-2017, 01:50 AM
RE: [SMALL QUESTIONS] - by Larz60+ - May-30-2017, 03:21 AM
RE: [SMALL QUESTIONS] - by failedSIGNAL - May-30-2017, 04:21 AM
RE: [SMALL QUESTIONS] - by buran - May-30-2017, 05:10 AM
RE: What is xrange? - by Ofnuts - Jun-01-2017, 09:50 AM
RE: What is xrange? - by ackmondual - Jun-13-2017, 11:27 PM
RE: What is xrange? - by nilamo - Jul-06-2017, 08:27 PM
RE: What is xrange? - by DeaD_EyE - Jul-07-2017, 09:22 AM

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020