As this popular question explains, range
objects in Python 3 are clever enough to be able to test efficiently for membership:
In [1]: 1000000000000000 in range(1000000000000001)
Out[1]: True # answer returned very quickly
However, the same is not true for the evaluation of a range
's maximum and minimum values with max
and min
, which seems to iterate over the entire sequence to find these values:
In [2]: max(range(1000000000000001)) # don't do this
...
It would be trivial to implement these functions efficiently for range
objects, so why hasn't it been done? Is there some implementation detail or edge case I am missing?