[ python-Bugs-1022912 ] Generator exps fail with large value of range

SourceForge.net noreply at sourceforge.net
Mon Sep 6 09:25:24 CEST 2004


Bugs item #1022912, was opened at 2004-09-06 02:19
Message generated for change (Comment added) made by rhettinger
You can respond by visiting: 
https://sourceforge.net/tracker/?func=detail&atid=105470&aid=1022912&group_id=5470

Category: Python Interpreter Core
Group: Python 2.4
>Status: Closed
>Resolution: Invalid
Priority: 5
Submitted By: Andy Elvey (mr_moose)
Assigned to: Nobody/Anonymous (nobody)
Summary: Generator exps fail with large value of range

Initial Comment:
  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
*********************************************

     




----------------------------------------------------------------------

>Comment By: Raymond Hettinger (rhettinger)
Date: 2004-09-06 02:25

Message:
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 ;-)

----------------------------------------------------------------------

You can respond by visiting: 
https://sourceforge.net/tracker/?func=detail&atid=105470&aid=1022912&group_id=5470


More information about the Python-bugs-list mailing list