2 ms·
That's not a good idiom, because it only allows you to loop over small ranges: >>> for i in range(1000000000): ... print i ... python2.4(1261) mal
by abstractbill 18y ago
That's not a good idiom, because it only allows you to loop over small ranges:
>>> for i in range(1000000000):
... print i
...
python2.4(1261) malloc: *** vm_allocate(size=4000002048) failed (error code=3)
python2.4(1261) malloc: *** error: can't allocate region
python2.4(1261) malloc: *** set a breakpoint in szone_error to debug
Traceback (most recent call last):
File "<stdin>", line 1, in ?
MemoryError
Why attempt to allocate such a large list when you just want to do something several times?
- oofoe 18y agofor i in xrange(1000000000): print i xrange() addresses the problem you describe and has been in Python since, like, forever. However, range() is nicer for learning the language, since it's easy to see how it composes into a list.
- abstractbill 18y agoYes, I know. It's just bad that people are taught one way of iterating, and then may or may not learn the right way at some later time.