This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: Generator exps fail with large value of range
Type: Stage:
Components: Interpreter Core Versions: Python 2.4
process
Status: closed Resolution: not a bug
Dependencies: Superseder:
Assigned To: Nosy List: mr_moose, rhettinger
Priority: normal Keywords:

Created on 2004-09-06 07:19 by mr_moose, last changed 2022-04-11 14:56 by admin. This issue is now closed.

Messages (2)
msg22368 - (view) Author: Andy Elvey (mr_moose) Date: 2004-09-06 07:19
  My platform - Mepis Linux, kernel 2.6.4 , Pentium III 

  I tested generator expressions by using the following
statement (which finds the sum of a range of numbers) - 

  sum(a for a in range(1, 123))

  As can be seen in the output below, this works fine
for small values of a.  However, it gives an
OverflowError for large values of a.  

********* Start of output
*******************************************

  Python 2.4a3 (#1, Sep  5 2004, 15:03:34)
[GCC 3.3.3 (Debian 20040429)] on linux2
Type "help", "copyright", "credits" or "license" for
more information.
>>> sum(a for a in range(1, 123))
7503
>>> sum(a for a in range(1, 999999999999999))
Traceback (most recent call last):
  File "<stdin>", line 1, in ?
OverflowError: range() result has too many items
>>> 

*********** End of output
*********************************************

     


msg22369 - (view) Author: Raymond Hettinger (rhettinger) * (Python committer) Date: 2004-09-06 07:25
Logged In: YES 
user_id=80475

That is an error message from range(), not from genexps.
To see that, try range(9999999999999) by itself.

Even if range could process numbers that big, you wouldn't
have enough memory to holding the resulting list.  For what
you're trying to do, use xrange() to save memory.

Also remember that using large numbers will result in that
many iterations.  If the number is too large, you may grow
old before the computation completes ;-)
History
Date User Action Args
2022-04-11 14:56:06adminsetgithub: 40874
2004-09-06 07:19:35mr_moosecreate