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


Groups > comp.lang.python > #93639

Re: Evaluation order

References <436cb6ac-59a4-44f1-be60-cdec1e509296@googlegroups.com> <mailman.379.1436493764.3674.python-list@python.org> <ea920a46-804f-45dd-9c6b-1fb4c278be49@googlegroups.com>
Date 2015-07-11 00:27 +1000
Subject Re: Evaluation order
From Chris Angelico <rosuav@gmail.com>
Newsgroups comp.lang.python
Message-ID <mailman.396.1436538455.3674.python-list@python.org> (permalink)

Show all headers | View raw


On Fri, Jul 10, 2015 at 10:04 PM, candide <c.candide@laposte.net> 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=[]):
    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 object.
>
> So, since the Python interpreter is performing evaluation from left to right, the first operand of the expression :
>
> t*(1+int(bool(t.sort())))
>
> evaluates to [2020, 42, 2015]. Next, the second operatand evaluates to the integer 1. So I was expecting the result to be a shallow copy of the first 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 side effect and it is as if the first operand has been evaluated twice before 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 = []
        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

Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

Evaluation order candide <c.candide@laposte.net> - 2015-07-09 17:10 -0700
  Re: Evaluation order Terry Reedy <tjreedy@udel.edu> - 2015-07-09 21:45 -0400
  Re: Evaluation order Chris Angelico <rosuav@gmail.com> - 2015-07-10 12:02 +1000
    Re: Evaluation order candide <c.candide@laposte.net> - 2015-07-10 05:04 -0700
      Re: Evaluation order Ned Batchelder <ned@nedbatchelder.com> - 2015-07-10 05:19 -0700
      Re: Evaluation order Terry Reedy <tjreedy@udel.edu> - 2015-07-10 08:27 -0400
      Re: Evaluation order Chris Angelico <rosuav@gmail.com> - 2015-07-11 00:27 +1000
      Re: Evaluation order Mark Lawrence <breamoreboy@yahoo.co.uk> - 2015-07-10 15:49 +0100

csiph-web