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

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

Why or why not?

12条回答
  •  深忆病人
    2020-11-22 14:25

    range() returns a list, xrange() returns an xrange object.

    xrange() is a bit faster, and a bit more memory efficient. But the gain is not very large.

    The extra memory used by a list is of course not just wasted, lists have more functionality (slice, repeat, insert, ...). Exact differences can be found in the documentation. There is no bonehard rule, use what is needed.

    Python 3.0 is still in development, but IIRC range() will very similar to xrange() of 2.X and list(range()) can be used to generate lists.

提交回复
热议问题