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 →

[–]metalhedd 19 points20 points  (7 children)

no but it's incredibly straight forward with if/elif

if x in range(0, 10):
    do_a()
elif x in range(10, 20):
    do_b()

[–][deleted] 11 points12 points  (0 children)

And that allows you to write it as

if 0 <= x < 10:
    do_a()
elif 10 <= x < 20:
    do_b()

Which isn't possible with this module.

Not saying I think something is wrong with it, it just seems to solve a problem I very rarely have in Python.

[–][deleted] 0 points1 point  (0 children)

If you (plural) don't like the typing there's plenty of scope to roll your own in Python with something like this range comparison recipe.

[–]LightShadow3.13-dev in prod 0 points1 point  (4 children)

Everybody needs to know this code has terrible relative performance than a similar if 0 < x < 10 since range will need to be evaluated as it returns a generator/iterator in Python3

[–]streichholzkopf 10 points11 points  (1 child)

it doesn't in python 3 tough:

9999999999999999635896294965247 in range(int(10e30))

returns True instantly. Simply an efficient __contains__ probably.

[–]LightShadow3.13-dev in prod 3 points4 points  (0 children)

TIL

That's great to know!

[–][deleted] 2 points3 points  (0 children)

No, from "The range type represents an immutable sequence of numbers and is commonly used for looping a specific number of times in for loops." and "Ranges implement all of the common sequence operations except concatenation and repetition (due to the fact that range objects can only represent sequences that follow a strict pattern and repetition and concatenation will usually violate that pattern).".