Python3's range objects support O(1) containment checking for integers (1) (2)
So one can do 15 in range(3, 19, 2)
and get the correct answer True
However, it doesn't support containment checking b/w two ranges
a = range(0, 10)
b = range(3, 7)
a in b # False
b in a # False, even though every element in b is also in a
a < b # TypeError: '<' not supported between instances of 'range' and 'range'
It seems that b in a
is interpreted as is any element in the range 'a' equal to the object 'b'?
However, since the range cannot contain anything but integers, range(...) in range(...)
will always return False
. IMHO, such a query should be answered as is every element in range 'b' also in range 'a'? Given that range only stores the start, stop, step and length, this query can also be answered in O(1).
The slice object doesn't help either. It doesn't implement the __contains__
method, and the __lt__
method simply compares two slices as tuples (which makes no sense)
Is there a reason behind the current implementation of these, or is it just a "it happened to be implemented this way" thing?