Evaluation order

candide c.candide at laposte.net
Fri Jul 10 08:04:25 EDT 2015


Le vendredi 10 juillet 2015 04:02:56 UTC+2, Chris Angelico a écrit :


 
> I'm not sure what contradiction you're referring to, here. The
> evaluation that you're pointing out says, as Terry showed via the
> disassembly, that Python's first action is to look up the name 't' and
> grab a reference to whatever object it points to. 


But in order to perform an operation, the interpreter has to evaluate the operands and "evaluating" is not "grabbing a reference to". 

> The execution of
> t.sort() has to happen before the multiplication, because of the
> parentheses.
>



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.







More information about the Python-list mailing list