-
-
Notifications
You must be signed in to change notification settings - Fork 32k
bpo-32856: Optimize the idiom for assignment in comprehensions. #5695
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
bpo-32856: Optimize the idiom for assignment in comprehensions. #5695
Conversation
Now `for y in [expr]` in comprehensions is so fast as a simple assignment `y = expr`.
break; | ||
case Tuple_kind: | ||
elts = gen->iter->v.Tuple.elts; | ||
break; |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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.
@@ -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``. |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
Lib/test/test_dictcomps.py
Outdated
@@ -81,6 +81,17 @@ def test_illegal_assignment(self): | |||
compile("{x: y for y, x in ((1, 2), (3, 4))} += 5", "<test>", | |||
"exec") | |||
|
|||
def test_assignment_idiom_in_comprehesions(self): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Typo: comprehesions
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fixed.
Now
for y in [expr]
in comprehensions is so fast as a simple assignmenty = expr
.https://bugs.python.org/issue32856