Evaluation order

Terry Reedy tjreedy at udel.edu
Thu Jul 9 21:45:56 EDT 2015


On 7/9/2015 8:10 PM, candide wrote:
> The official doc explains that :
> Python evaluates expressions from left to right.
> cf. https://docs.python.org/3.3/reference/expressions.html#evaluation-order


> But consider the following snippet :
>
>>>> t=[2020, 42, 2015]
>>>> t*(1+int(bool(t.sort())))
> [42, 2015, 2020]

> Is there not some contradiction with left-right evalutation?

No, as shown by the disassembled byte code
 >>> dis.dis("t*(1+int(bool(t.sort())))")
   1           0 LOAD_NAME            0 (t)
               3 LOAD_CONST           0 (1)
               6 LOAD_NAME            1 (int)
               9 LOAD_NAME            2 (bool)
              12 LOAD_NAME            0 (t)
              15 LOAD_ATTR            3 (sort)
              18 CALL_FUNCTION        0 (0 positional, 0 keyword pair)
              21 CALL_FUNCTION        1 (1 positional, 0 keyword pair)
              24 CALL_FUNCTION        1 (1 positional, 0 keyword pair)
              27 BINARY_ADD
              28 BINARY_MULTIPLY
              29 RETURN_VALUE

t.sort() sorts in place and returns None

-- 
Terry Jan Reedy




More information about the Python-list mailing list