Path: csiph.com!usenet.pasdenom.info!weretis.net!feeder1.news.weretis.net!feeder.erje.net!1.eu.feeder.erje.net!newsfeed.xs4all.nl!newsfeed7.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; 'operand': 0.07; 'cc:addr :python-list': 0.09; 'atom': 0.09; 'question?': 0.09; 'python': 0.10; 'def': 0.13; 'interpreter': 0.15; '42,': 0.16; 'count,': 0.16; 'evaluates': 0.16; 'evaluating': 0.16; 'from:addr:rosuav': 0.16; 'from:name:chris angelico': 0.16; 'operands': 0.16; 'operation,': 0.16; 'operation.': 0.16; 'performed.': 0.16; 'shallow': 0.16; 'skip:g 50': 0.16; 'stack;': 0.16; 'wrote:': 0.16; 'obviously': 0.16; 'integer': 0.18; '2015': 0.20; 'cc:2**0': 0.20; 'cc:addr:python.org': 0.20; 'work,': 0.21; '(the': 0.22; 'next,': 0.22; 'object.': 0.22; 'suppose': 0.22; 'performing': 0.23; 'this:': 0.23; 'second': 0.24; 'header:In-Reply-To:1': 0.24; 'sort': 0.25; 'fri,': 0.27; 'parameters': 0.27; 'message- id:@mail.gmail.com': 0.27; 'object,': 0.27; 'function': 0.28; 'values': 0.28; '*the': 0.29; 'yields': 0.29; 'code': 0.30; "can't": 0.32; 'skip:_ 10': 0.32; 'operate': 0.32; 'point': 0.33; 'class': 0.33; 'running': 0.34; 'list': 0.34; 'received:google.com': 0.35; 'so,': 0.35; 'execution': 0.35; 'identity': 0.35; 'list:': 0.35; 'something': 0.35; "isn't": 0.35; 'but': 0.36; 'instead': 0.36; '(and': 0.36; 'evaluation': 0.36; 'pm,': 0.36; 'subject:: ': 0.37; 'two': 0.37; 'does': 0.39; 'takes': 0.39; 'where': 0.40; 'some': 0.40; 'side': 0.62; 'is.': 0.63; 'account': 0.66; 'jul': 0.72; 'evaluate': 0.72; 'obvious': 0.76; 'actually,': 0.84; 'chrisa': 0.84; 'to:none': 0.91 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:date:message-id:subject:from:cc :content-type:content-transfer-encoding; bh=zDNhSiQAEsWHn+UXiED6bhtl/8/sro8Zaqc4AtiLKTQ=; b=Gj3CsnKTsx7jPcCe9wjKo3aTO4F3bC4IBP1SVEVbdo6ze2S+Z3ZvyXl6cBBUn5bKf0 ayyItKlK70ieO0hUkMKy6+HJ4Jow8wnebJdDq0iFRVCmOUqwO2kWua8LnPy1N+CHDD8r agzHajEsCOvF26wWEBNIsiHovJdyQrOxwSCV5JbungBJpYNSjFr/h9DVbIRMmbgTocfG luNXts01ucCDBNoAMt/GYz7wjzh6bWsyaDTNt5oDmNnsMSrAp+tsCEzs3MlpCRLKVc20 3PEvtOk8QJUoMNDvU8uj0kNVcf4P9esbUd7tCfvYxYObL4T5qpmfVeiULbI/QwGvLE5t yE2Q== MIME-Version: 1.0 X-Received: by 10.50.114.9 with SMTP id jc9mr3739765igb.56.1436538452489; Fri, 10 Jul 2015 07:27:32 -0700 (PDT) In-Reply-To: References: <436cb6ac-59a4-44f1-be60-cdec1e509296@googlegroups.com> Date: Sat, 11 Jul 2015 00:27:32 +1000 Subject: Re: Evaluation order From: Chris Angelico Cc: "python-list@python.org" Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: quoted-printable X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.20+ 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: 57 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1436538455 news.xs4all.nl 2847 [2001:888:2000:d::a6]:60637 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:93639 On Fri, Jul 10, 2015 at 10:04 PM, candide wrote: > But in order to perform an operation, the interpreter has to evaluate the= operands and "evaluating" is not "grabbing a reference to". Actually, it is. Suppose that instead of 't', you had a function call: def get_t(announce, returnme=3D[]): print(announce) return returnme get_t("setup").extend((2020, 42, 2015)) get_t("first")*(1+int(bool(get_t("second").sort()))) I think it's obvious from running the above code that get_t("first") is evaluated before get_t("second") is. In the trivial case where the operand is a simple name, yes, evaluating that name is simply "grab a reference to the object this name references" (and in the case of CPython, shove it onto the stack; other Pythons may operate some other way). > Official docs explains what evaluation is : > > When the name is bound to an object, evaluation of the atom yields that o= bject. > > So, since the Python interpreter is performing evaluation from left to ri= ght, the first operand of the expression : > > t*(1+int(bool(t.sort()))) > > evaluates to [2020, 42, 2015]. Next, the second operatand evaluates to th= e integer 1. So I was expecting the result to be a shallow copy of the firs= t list [2020, 42, 2015] (the value of t before side effect produced by the = sort method). On the contrary, the final result takes into in account the s= ide effect and it is as if the first operand has been evaluated twice befor= e execution of the multiplication operation. > The shallow copy isn't made until the multiplication is performed; it's only at that point that a function takes two parameters and does the work, something like this: class list: def __mul__(self, count): result =3D [] for i in range(count): result.extend(self) return result Obviously you can't call that function until you know both the list object and the count, so no copies are made until both those values can be provided. The first operand evaluates to *the list with the identity X*, the second operand evaluates to *the integer 1*, and then the multiplication is performed. Does that answer the question? ChrisA