On Fri, Jul 10, 2015 at 10:04 PM, candide <c.cand...@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 -- https://mail.python.org/mailman/listinfo/python-list