osdir.com


[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[Python-Dev] Examples for PEP 572


2018-07-04 9:58 GMT+02:00 Serhiy Storchaka <storchaka at gmail.com>:>
04.07.18 05:42, Steven D'Aprano ????:
>> There is a deferred feature request to optimize "for x in [item]" as
>> equivalent to "for x in (item,)", to avoid constructing a list:
>>
>> https://bugs.python.org/issue32856
>
>
> No, this optimization was already made in issue32925.

Oh, I missed that optimization: it's even optimized at the AST level, cool!

Good job INADA Naoki for the new AST optimizer and Serhiy for this optimization!

$ ./python
Python 3.8.0a0 (heads/master:97ae32c92e, Jul  4 2018, 09:37:54)
>>> import dis; dis.dis('x in [y]')
  1           0 LOAD_NAME                0 (x)
              2 LOAD_NAME                1 (y)
              4 BUILD_TUPLE              1
              6 COMPARE_OP               6 (in)
              8 RETURN_VALUE

Note: "x in [1]" was already optimized as "x in (1,)" since at least
Python 2.7 (in the bytecode peephole optimizer, not at the AST level).

Victor