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

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

Why or why not?

12条回答
  •  攒了一身酷
    2020-11-22 14:13

    No, they both have their uses:

    Use xrange() when iterating, as it saves memory. Say:

    for x in xrange(1, one_zillion):
    

    rather than:

    for x in range(1, one_zillion):
    

    On the other hand, use range() if you actually want a list of numbers.

    multiples_of_seven = range(7,100,7)
    print "Multiples of seven < 100: ", multiples_of_seven
    

提交回复
热议问题