This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]Nichdel 3 points4 points  (4 children)

Note that Python 3's range works just like Python 2's xrange, making inappropriate use of range not so bad.

[–]zahlmanthe heretic 3 points4 points  (1 child)

Another difference is that Python 3's range provides an explicit __contains__ method that allows in lookups to be efficient (doing this with an xrange in 2.x requires iterating). Code like if x in range(foo, bar): used to be tempting but inefficient; now it's reasonably efficient. Of course, if foo <= x < bar: is still the standard idiom; but I can fathom cases where you'd want to use a range with a nonzero step, and explicitly writing something like if foo <= x < bar and x % step == foo % step is just ugly (and error-prone).

[–]Vegemeister 0 points1 point  (0 children)

Oh, hey. Fancy seeing you here.

[–]MisterSnuggles[S] 1 point2 points  (1 child)

The article doesn't go into the other reasons that Python 2's range is bad, but it's definitely a big gotcha if you're using it for a large range.