You can restore the original start and step arguments, but not always the original stop argument.
One way to get the values is to look at the __reduce__ (or __reduce_ex__ ) method of the xrange object (usually used for etching):
>>> 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] will assign the three variable names to the corresponding integers. The start and step values are always the original values that were used to build the xrange object.
stop may be higher than the argument originally used to compress the object. For any xrange x object, it is x[-1] + step .
In general, it is not possible to restore the original stop argument after creating the xrange object. If you examine the source for xrange , you will see that this object does not store a reference to a specific stop value, but only the start value, step value and total length of the iterator. When str or __reduce__ , the object will calculate the last possible stop value using the get_stop_for_range internal function.
This contrasts with the Python 3 range object, which remembers the original stop value.
source share