bpo-32856: Optimize the idiom for assignment in comprehensions. by serhiy-storchaka · Pull Request #5695 · python/cpython

@serhiy-storchaka

@serhiy-storchaka

Now `for y in [expr]` in comprehensions is so fast as a simple
assignment `y = expr`.

@serhiy-storchaka serhiy-storchaka changed the title bpo-32856: Optimiz the idiom for assignment in comprehensions. bpo-32856: Optimize the idiom for assignment in comprehensions.

Feb 16, 2018

1st1

break;
case Tuple_kind:
elts = gen->iter->v.Tuple.elts;
break;

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How about we handle for x in {a} too?

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This will change the behavior if a is not hashable.

1st1

@@ -0,0 +1,3 @@
Optimized the idiom for assignment a temporary variable in comprehensions.
Now ``for y in [expr]`` in comprehensions is so fast as a simple assignment
``y = expr``.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please update the NEWS entry with some benchmark results.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure what should I provide. The optimized code always is the part of complex comprehension expression.

@serhiy-storchaka

cpitclaudel

compile("{x: y for y, x in ((1, 2), (3, 4))} += 5", "<test>",
"exec")

def test_assignment_idiom_in_comprehesions(self):

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Typo: comprehesions

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed.

@serhiy-storchaka

@serhiy-storchaka

@serhiy-storchaka