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

Chris Angelico rosuav at gmail.com
Sun Mar 31 18:32:04 EDT 2013


On Mon, Apr 1, 2013 at 9:28 AM, Chris Angelico <rosuav at gmail.com> wrote:
> On Mon, Apr 1, 2013 at 9:06 AM, Alex <foo at email.invalid> wrote:
>> 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.
>
> http://docs.python.org/3/reference/expressions.html#operator-precedence
>
> Opening paragraph, "... exponentiation, which groups from right to
> left". It follows the obvious expectation from mathematics. (The OP is
> using Python 2, but the same applies.)

Though your point about 6.14 is still true. It states the order that
the integers will be evaluated in. Note one of the examples given:

expr1 + expr2 * (expr3 - expr4)

The evaluation of the expression starts with 3 and 4, then picks up 2,
then 1, but the operands themselves are evaluated left to right.

ChrisA



More information about the Python-list mailing list