3

It seems that both exceptions are raised in similar situation.

What is the difference and what happens behind the scenes in each of these two code lines?

>>> (i for i in range(1000000000)) # 10^9
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
MemoryError
>>> (i for i in range(10000000000)) # 10^10
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
OverflowError: range() result has too many items

2 Answers 2

4

In the first example, you try to pass a list of 1,000,000,000 integers to the generator expression. You computer runs out of memory trying to create that list. In Python 3, I suspect this would work, since range itself produces a generator instead of an explicit list.

In the second example, I suspect range requires a 32-bit value for its input on your machine, and so throws a different error before it can run out of memory.

Sign up to request clarification or add additional context in comments.

1 Comment

You are right - I use 32 bit python. 1000000000 < 2^32 < 10000000000
2

From the docs:

Exception OverflowError

Raised when the result of an arithmetic operation is too large to be represented. This cannot occur for long integers (which would rather raise MemoryError than give up) and for most operations with plain integers, which return a long integer instead. Because of the lack of standardization of floating point exception handling in C, most floating point operations also aren’t checked.

Exception MemoryError

Raised when an operation runs out of memory but the situation may still be rescued (by deleting some objects). The associated value is a string indicating what kind of (internal) operation ran out of memory. Note that because of the underlying memory management architecture (C’s malloc() function), the interpreter may not always be able to completely recover from this situation; it nevertheless raises an exception so that a stack traceback can be printed, in case a run-away program was the cause.

In Python 2, range(1000000000) produces a list with 1000000000 elements, with causes you to run out of memory. If you replace range with xrange, the problem goes away because you are using a generator instead.

In the second example, 1000000000 is actually a long. If you use xrange, you get:

>>> (i for i in xrange(10000000000))
OverflowError: long int too large to convert to int

1 Comment

But the first example produces the MemoryError and vice versa.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.