Live data from Hacker News

What the heck is an xrange?

late.am

21–28 of 28 posts

Re: What the heck is an xrange?

#21
post #19

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…

That imho seems like overcomplicating a relatively straight-forward piece of code without noticeable improvement. In the original code the intent is clear from the first line ("check the number of arguments"), but in your version I have to parse 7 lines of code before I find out that. Also in your code I'm required to keep larger, more complicated state (the map array) in my head when reading it. Your version also looks like it would perform worse than the original. Imho code that looks like it performs suboptimally is code that looks ugly, even if the performance difference in reality would be negligible.

Re: What the heck is an xrange?

#24
post #2

Nitpicking, 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…

You can also cast to long, which doesn't lose precision.

Re: What the heck is an xrange?

#25
I 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.

Re: What the heck is an xrange?

#26
post #9
post #5

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

If you put two spaces at the beginning of a line, you'll get a monospaced "literal" mode.

  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?

#27
post #20
post #19

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…

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…

If only lambda's allowed statements :(.

  >>>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?

#28
post #25

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

But there's a good motivation here: (x)range objects are supposed to be index-able, while a generator (expression) cannot go back once an element has been consumed.
Post reply on HN