Path: csiph.com!usenet.pasdenom.info!weretis.net!feeder1.news.weretis.net!feeder.erje.net!eu.feeder.erje.net!newsfeed.freenet.ag!news2.euro.net!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.002 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'operator': 0.03; 'assignment': 0.07; 'expressions': 0.07; 'expectation': 0.09; 'paragraph,': 0.09; 'raises': 0.09; 'subject:Why': 0.09; 'python': 0.11; '"python': 0.16; 'assignments,': 0.16; 'discusses': 0.16; 'evaluates': 0.16; 'evaluating': 0.16; 'from:addr:rosuav': 0.16; 'from:name:chris angelico': 0.16; 'mathematics.': 0.16; 'operators.': 0.16; 'order)': 0.16; 'really?': 0.16; 'rhs': 0.16; 'unary': 0.16; 'url:html)': 0.16; 'exception': 0.16; 'wrote:': 0.18; 'alex': 0.19; '(the': 0.22; 'mon,': 0.24; 'first,': 0.26; 'header:In-Reply-To:1': 0.27; 'specifically': 0.29; 'am,': 0.29; "doesn't": 0.30; 'evaluation': 0.30; 'message-id:@mail.gmail.com': 0.30; '"",': 0.31; 'calculated': 0.31; 'follows': 0.31; 'anyone': 0.31; 'file': 0.32; 'says': 0.33; 'url:python': 0.33; '(most': 0.33; 'sense': 0.34; 'received:209.85': 0.35; 'except': 0.35; 'but': 0.35; 'received:google.com': 0.35; 'done': 0.36; "didn't": 0.36; 'subject:?': 0.36; 'url:org': 0.36; 'received:209': 0.37; 'being': 0.38; 'to:addr:python-list': 0.38; 'recent': 0.39; 'does': 0.39; 'realize': 0.39; 'to:addr:python.org': 0.39; 'skip:u 10': 0.60; 'dave': 0.60; 'expression': 0.60; 'url:3': 0.61; 'obvious': 0.74; 'power': 0.76; 'url:reference': 0.84; '6.4': 0.91; 'angel': 0.91; '2013': 0.98 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:x-received:in-reply-to:references:date:message-id :subject:from:to:content-type; bh=oWNCZZBLauY9Ekz55W6ohdmc4+I7szrk5yGvmmJwIY4=; b=IPXxRe5fTKZQsWlG+VIsMO7/hH0nwtrx1h7TNj/hKzcSDbhXK84KocRIpfObLWP8oC K3IkCxyxrF+x0l05tT5G1uupThS0Ab8snArE2hvCWl32jwtks1nO6PQzRvC5lJnMw3mV MeFtcur7rS3bY4Gf99b9SJNN/kdMTUrXXnfPZNl4SaotRhccVGkYJ85MXckMsdcfVcPt CV+zUk+jOzv+HHPQvOxEas/Dj4h4rqqgFzRDYVoK9ZhW7rYlsSmN+69M9z0Fv3uSpmZh I4ZSwj+naLGa2dA6LTpnGehwKv0KaI6BqYGEbmwwPDn+BMcwNyocZXwWiCM4MQU3IX6U czCA== MIME-Version: 1.0 X-Received: by 10.58.253.161 with SMTP id ab1mr7442984ved.55.1364768897459; Sun, 31 Mar 2013 15:28:17 -0700 (PDT) In-Reply-To: References: <8e43bc87-e822-4bb3-b9ef-ccd489da8bf3@googlegroups.com> Date: Mon, 1 Apr 2013 09:28:17 +1100 Subject: Re: Why does 1**2**3**4**5 raise a MemoryError? From: Chris Angelico To: python-list@python.org Content-Type: text/plain; charset=ISO-8859-1 X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 41 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1364768905 news.xs4all.nl 6896 [2001:888:2000:d::a6]:33613 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:42429 On Mon, Apr 1, 2013 at 9:06 AM, 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 "", line 1, in >> > 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. 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.) ChrisA