Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #42357 > unrolled thread

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

Started bymorphex <morphex@gmail.com>
First post2013-03-30 23:56 -0700
Last post2013-03-31 18:34 -0400
Articles 2 on this page of 22 — 11 participants

Back to article view | Back to comp.lang.python


Contents

  Why does 1**2**3**4**5 raise a MemoryError? morphex <morphex@gmail.com> - 2013-03-30 23:56 -0700
    Re: Why does 1**2**3**4**5 raise a MemoryError? Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2013-03-31 07:33 +0000
      Re: Why does 1**2**3**4**5 raise a MemoryError? Dave Angel <davea@davea.name> - 2013-03-31 03:48 -0400
      Re: Why does 1**2**3**4**5 raise a MemoryError? morphex <morphex@gmail.com> - 2013-03-31 05:07 -0700
        Re: Why does 1**2**3**4**5 raise a MemoryError? Dave Angel <d@davea.name> - 2013-03-31 08:43 -0400
          Re: Why does 1**2**3**4**5 raise a MemoryError? Roy Smith <roy@panix.com> - 2013-03-31 09:15 -0400
            Re: Why does 1**2**3**4**5 raise a MemoryError? Jason Swails <jason.swails@gmail.com> - 2013-03-31 10:03 -0400
        Re: Why does 1**2**3**4**5 raise a MemoryError? Roy Smith <roy@panix.com> - 2013-03-31 09:04 -0400
        Re: Why does 1**2**3**4**5 raise a MemoryError? Tim Roberts <timr@probo.com> - 2013-04-01 21:45 -0700
          Re: Why does 1**2**3**4**5 raise a MemoryError? Chris Angelico <rosuav@gmail.com> - 2013-04-02 17:16 +1100
          Re: Why does 1**2**3**4**5 raise a MemoryError? Dan Sommers <dan@tombstonezero.net> - 2013-04-02 12:56 +0000
      Re: Why does 1**2**3**4**5 raise a MemoryError? Roy Smith <roy@panix.com> - 2013-03-31 08:59 -0400
    Re: Why does 1**2**3**4**5 raise a MemoryError? Dave Angel <davea@davea.name> - 2013-03-31 03:34 -0400
      Re: Why does 1**2**3**4**5 raise a MemoryError? "Alex" <foo@email.invalid> - 2013-03-31 22:06 +0000
        Re: Why does 1**2**3**4**5 raise a MemoryError? Chris Angelico <rosuav@gmail.com> - 2013-04-01 09:28 +1100
          Re: Why does 1**2**3**4**5 raise a MemoryError? "Alex" <foo@email.invalid> - 2013-04-01 00:39 +0000
            Re: Why does 1**2**3**4**5 raise a MemoryError? Chris Angelico <rosuav@gmail.com> - 2013-04-01 11:58 +1100
            Re: Why does 1**2**3**4**5 raise a MemoryError? Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2013-04-01 04:16 +0000
              Re: Why does 1**2**3**4**5 raise a MemoryError? Roy Smith <roy@panix.com> - 2013-04-01 07:48 -0400
            Re: Why does 1**2**3**4**5 raise a MemoryError? Nobody <nobody@nowhere.com> - 2013-04-02 08:40 +0100
        Re: Why does 1**2**3**4**5 raise a MemoryError? Chris Angelico <rosuav@gmail.com> - 2013-04-01 09:32 +1100
        Re: Why does 1**2**3**4**5 raise a MemoryError? Dave Angel <davea@davea.name> - 2013-03-31 18:34 -0400

Page 2 of 2 — ← Prev page 1 [2]


#42430

FromChris Angelico <rosuav@gmail.com>
Date2013-04-01 09:32 +1100
Message-ID<mailman.4039.1364769131.2939.python-list@python.org>
In reply to#42424
On Mon, Apr 1, 2013 at 9:28 AM, Chris Angelico <rosuav@gmail.com> wrote:
> On Mon, Apr 1, 2013 at 9:06 AM, Alex <foo@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

[toc] | [prev] | [next] | [standalone]


#42431

FromDave Angel <davea@davea.name>
Date2013-03-31 18:34 -0400
Message-ID<mailman.4040.1364769267.2939.python-list@python.org>
In reply to#42424
On 03/31/2013 06:06 PM, Alex wrote:
> 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
>

On the page you reference, in section 6.14, see the 4th entry:

expr1 + expr2 * (expr3 - expr4)

expr1 is evaluated before expr2, but the multiply happens before the add.

Now see the following paragraph (6.15):

"""
The following table summarizes the operator precedences in Python, from 
lowest precedence (least binding) to highest precedence (most binding). 
Operators in the same box have the same precedence. Unless the syntax is 
explicitly given, operators are binary. Operators in the same box group 
left to right (except for comparisons, including tests, which all have 
the same precedence and chain from left to right — see section 
Comparisons — and exponentiation, which groups from right to left).
"""

What this paragraph refers to as "grouping" is commonly called 
associativity in other languages.

There are three different concepts here that apply whenever an 
expression has more than one term:

1) evaluation order is important for terms lie function calls which have 
side effects
2) precedence, lie where multiply will happen before add
3) associativity, where the order of operations for operators with the 
same precedence are done either left to right (usually), or right to 
left (like exponentiation)




-- 
DaveA

[toc] | [prev] | [standalone]


Page 2 of 2 — ← Prev page 1 [2]

Back to top | Article view | comp.lang.python


csiph-web