Why does 1**2**3**4**5 raise a MemoryError?

Alex foo at email.invalid
Sun Mar 31 18:06:49 EDT 2013


Dave Angel wrote:

> On 03/31/2013 02:56 AM, morphex wrote:
> > > > > 1**2
> > 1
> > > > > 1**2**3
> > 1
> > > > > 1**2**3**4
> > 1L
> > > > > 1**2**3**4**5
> > Traceback (most recent call last):
> >   File "<stdin>", line 1, in <module>
> > MemoryError
> > > > > 
> > 
> > Does anyone know why this raises a MemoryError?  Doesn't make sense
> > to me.
> 
> Perhaps you didn't realize that the expression will be done from
> right to left.  

Really? 

The Python 3 documentation
(http://docs.python.org/3/reference/expressions.html) says in section
6.14 (Evaluation order) that "Python evaluates expressions from left to
right" (an exception being when evaluating assignments, in which case
the RHS of the assignment is calculated first, in left-to-right order).

Section 6.4 discusses the power operator specifically and does not
contradict 6.14 except that the power operator uses right-to-left
evaluation in the presence of unparenthesized unary operators.

Neither of these two exception cases appear to apply here, so I think
the OP is reasonable in expecting Python to do the operation
left-to-right.

Am I missing something written somewhere else in the docs? Are the docs
I quoted wrong? Please help me understand the discrepancy I am
perceiving here.

Alex



More information about the Python-list mailing list