[Python-Dev] Examples for PEP 572
vstinner at redhat.com
Wed Jul 4 05:17:03 EDT 2018
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:
> 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 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)
Note: "x in " was already optimized as "x in (1,)" since at least
Python 2.7 (in the bytecode peephole optimizer, not at the AST level).
More information about the Python-Dev