If s
is a slice
object in Python, constructed using either s = slice(start, stop, step)
or (in the appropriate context) start:stop:step
, the values used to construct s
are available from the slice
object itself as s.start
, s.stop
and s.step
.
Similar start
, stop
and step
members are available on range
objects in Python 3.4 [Issue9896]. For example, range(1, 4, 2).start == 1
.
However, Python 2.7 xrange
objects do not have the start
, stop
and step
members. Is there any other way to obtain the values used to construct the xrange
from the object itself?
The xrange() function in Python is used to generate a sequence of numbers, similar to the range() function. However, xrange() is used only in Python 2. x whereas range() is used in Python 3. x.
range() – This returns a range object (a type of iterable). xrange() – This function returns the generator object that can be used to display numbers only by looping. The only particular range is displayed on demand and hence called “lazy evaluation“.
Return type: range() – This returns a list of numbers created using range() function. xrange() – This function returns the generator object that can be used to display numbers only by looping. Only particular range is displayed on demand and hence called lazy evaluation .
xrange returns an iterator and only keeps one number in memory at a time. range keeps the entire list of numbers in memory.
It is possible to recover the original start
and step
arguments, but not always the original stop
argument.
One way you can get the values is to look at the __reduce__
(or __reduce_ex__
) method of the xrange
object (normally used for pickling):
>>> x = xrange(10)
>>> x.__reduce__()[1]
(0, 10, 1)
>>> y = xrange(2, 100, 13)
>>> y.__reduce__()[1]
(2, 106, 13)
Then start, stop, step = y.__reduce__()[1]
would assign the three variable names to the appropriate integers. The start
and step
values are always the original values that were used to construct the xrange
object.
stop
may be higher that the argument originally used to contract the object. For any xrange
object x
it is equal to x[-1] + step
.
In general, it is not possible to recover the original stop argument once the xrange
object has been created. If you study the source for xrange
, you'll see that this object does not keep a reference to a particular stop value, only the starting value, step value and overall length of the iterator. When str
or __reduce__
is called, the object will calculate the latest possible stop value with the internal function get_stop_for_range
.
This is in contrast to Python 3's range
object, which does remember the original stop value.
It's not pretty, but you could parse the str
representation of the xrange
>>> rr = xrange(1, 4, 2)
>>> str(rr)
'xrange(1, 5, 2)'
>>> start = str(rr).split('(')[1].split(',')[0]
>>> start
1
etc
Note that the max
cannot reliably be found this way if there are steps specified in the range. For example, in the example above, (1, 4, 2)
becomes (1, 5, 2)
in the str
representation.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With