Why is “1000000000000000 in range(1000000000000001)” so fast in Python 3?

前端 未结 11 1538
梦毁少年i
梦毁少年i 2020-11-22 03:46

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.

11条回答
  •  不要未来只要你来
    2020-11-22 04:25

    The other answers explained it well already, but I'd like to offer another experiment illustrating the nature of range objects:

    >>> r = range(5)
    >>> for i in r:
            print(i, 2 in r, list(r))
    
    0 True [0, 1, 2, 3, 4]
    1 True [0, 1, 2, 3, 4]
    2 True [0, 1, 2, 3, 4]
    3 True [0, 1, 2, 3, 4]
    4 True [0, 1, 2, 3, 4]
    

    As you can see, a range object is an object that remembers its range and can be used many times (even while iterating over it), not just a one-time generator.

提交回复
热议问题