[Python-3000] optimizing [x]range

skip at pobox.com skip at pobox.com
Mon Jul 30 04:17:24 CEST 2007


    Greg> I think the use cases for this are where you're trying to express
    Greg> a range-like condition, i.e 'a <= x < b'. Then you have to make
    Greg> sure you get the right relations in the right places, which is the
    Greg> same kind of burden as remembering to add or subtract 1 in the
    Greg> right places.

I think it's easier to learn that 'a <= x < b' is logically equivalent to 
'a <= x and x < b' than inferring that 'x in range(a, b)' means the same
thing.  In fact, due to shortcut semantics they actually don't mean quite
the same thing since b might not get evaluated in the cascading comparison
case.  Given that I find the cascading comparisons clearer I see no reason
to optimize the "in range(...)" case.

Skip


More information about the Python-3000 mailing list