It is my understanding that the range()
function, which is actually an object type in Python 3, generates its contents on the fly, similar to a generator.
The object returned by range()
is actually a range
object. This object implements the iterator interface so you can iterate over its values sequentially, just like a generator, list, or tuple.
But it also implements the __contains__ interface which is actually what gets called when an object appears on the right hand side of the in
operator. The __contains__()
method returns a bool
of whether or not the item on the left-hand-side of the in
is in the object. Since range
objects know their bounds and stride, this is very easy to implement in O(1).