Not worth a pull request, but personally I'd replace: if len(args) == 1: start, stop, step = 0, args[0], 1 elif len(args) == 2: start, stop, step = args[0], args[1], 1 elif len(args) == 3: start, stop, step = args else: raise TypeError('xrange() requires 1-3 int arguments') with: map = [ lambda args: (0, args[0], 1), lambda args: (args[0], args[1], 1), lambda args: args, ] try: start, stop, step = map[len(args)](args…
What the heck is an xrange?
21–28 of 28 posts
Re: What the heck is an xrange?
#22As a side-note, here's a related article on implementing a Python generator "for real" using the C API: http://eli.thegreenplace.net/2012/04/05/implementing-a-gener...
Re: What the heck is an xrange?
#23Answers to http://stackoverflow.com/questions/1482480/xrange2100-overfl... contain several implementations of xrange() in pure Python
Re: What the heck is an xrange?
#24Nitpicking, but this statement doesn't work with large numbers (which xrange() is supposed to handle correctly): self._len = int(ceil(float(stop - start) / step)) Python supports arbitrary-length integers; you can't just cast those to (fixed-length) floating point numbers without losing precision. It's better to use integer division here, for example: self._len = (stop - start)//step + bool((stop - start)%step) (A va…
Re: What the heck is an xrange?
#25Re: What the heck is an xrange?
#26Earlier quoted context omitted.
Huh -- CPython 2.x doesn't let you create an xrange with values past 2 63-1. CPython 3.x does.
Even 63-bit integers aren't (all) representable in IEEE double floating point values (that Python uses) which have a 53 bits mantissa. For example, int(float(10¹⁸ - 1)) != 10¹⁸ - 1, but xrange(1, 10¹⁸) is perfectly valid (even in Python 2). edit : how do I type two consecutive asterisks on Hacker News? Backslash doesn't seem to work as an escape character.
For example, int(float(10**18 - 1)) != 10**18 - 1,
but xrange(1, 10**18) is perfectly valid (even in Python 2).Re: What the heck is an xrange?
#27Not worth a pull request, but personally I'd replace: if len(args) == 1: start, stop, step = 0, args[0], 1 elif len(args) == 2: start, stop, step = args[0], args[1], 1 elif len(args) == 3: start, stop, step = args else: raise TypeError('xrange() requires 1-3 int arguments') with: map = [ lambda args: (0, args[0], 1), lambda args: (args[0], args[1], 1), lambda args: args, ] try: start, stop, step = map[len(args)](args…
To me your version is less clearer than the explicit if calls: * Name 'map' for a variable is a poor choice (as it has same name as the python builtin function map) * Your version has off by one error, it doesn't give correct results when called with a single element list or if the list has three elements: >>>mymap = [ lambda args: (0, args[0], 1), lambda args: (args[0], args[1], 1), lambda args: args, ] >>>args = [5…
>>>mymap = [
lambda args: raise IndexError,
lambda args: (0, args[0], 1),
lambda args: (args[0], args[1], 1),
lambda args: args,
]
That aside, this approach would be much slower too.Re: What the heck is an xrange?
#28I cried a little bit on the inside when he said that that the iterator should not be implemented using generators, etc. Writing iterators by hand forces one to turn the iteration code inside out and it can be a painful experience if the logic is anything nontrivial.