Should you always favor xrange() over range()?

后端 未结 12 2120
后悔当初
后悔当初 2020-11-22 13:36

Why or why not?

12条回答
  •  慢半拍i
    慢半拍i (楼主)
    2020-11-22 14:33

    You should favour range() over xrange() only when you need an actual list. For instance, when you want to modify the list returned by range(), or when you wish to slice it. For iteration or even just normal indexing, xrange() will work fine (and usually much more efficiently). There is a point where range() is a bit faster than xrange() for very small lists, but depending on your hardware and various other details, the break-even can be at a result of length 1 or 2; not something to worry about. Prefer xrange().

提交回复
热议问题