Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I get the values used to construct a Python 2.7 xrange from the object itself?

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?

like image 536
user200783 Avatar asked Apr 21 '15 09:04

user200783


People also ask

What is Xrange () Python?

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.

What is the difference between Range () and Xrange () in Python 2?

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“.

How do you use range () and Xrange () functions in pandas?

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 .

What's the difference between Xrange and range?

xrange returns an iterator and only keeps one number in memory at a time. range keeps the entire list of numbers in memory.


2 Answers

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.

like image 187
Alex Riley Avatar answered Sep 29 '22 13:09

Alex Riley


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.

like image 23
jlb83 Avatar answered Sep 29 '22 11:09

jlb83