-
-
Notifications
You must be signed in to change notification settings - Fork 31.9k
Simplify some while-loops with walrus operator #90133
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
Comments
The following pattern occurs a few times in the codebase: while 1:
data = conn.recv(blocksize)
if not data:
break
... There is an unbounded while loop in which some kind of input is read. If the input is there, it is processed and the loop is continued; otherwise the loop is broken. This can be expressed more cleanly with the walrus operator: while data := conn.recv(blocksize):
... Some of this code goes back to the days of Python 1. I assume that the original authors would have used the walrus idiom if it had been available, which it wasn't. But now it is. Rewriting the instances of this pattern shaves off 148 lines from the codebase. Removing the manual break statements makes the logic more straightforward. This should not change the behavior of anything. I'm assuming that this code is all under test and that any deviations from the existing behavior will be caught. Anything that isn't tested shouldn't be changed (and should be tested). |
As a general rule, refactorings like this tend to be rejected as the risk of inadvertently adding a new bug outweighs the benefit of subjectively cleaner code ("if it ain't broke, don't fix it!"). Is there any measurable performance benefit from this? Reducing several hundred thousand lines of code by 148 is not a compelling benefit :) |
The general rule as stated by Zachary is correct; however, I'm in mildly favor of this PR because these are the use cases that the walrus operator was specifically designed for. That said, it would be nice to verify that timings don't get worse and to carefully review each edit to make sure it doesn't introduce a bug. |
As I mentioned in the PR, merging this can make future backports to 3.6 and 3.7 more complicated. |
That's correct, but it is also true that we do very few of those and the likelihood of a conflict with one of these edits is low. |
I wouldn't expect any performance changes either way. If it worked out to be slower, that would an unpleasant surprise and a good reason to reject this change. If it worked out to be faster, well, that would be great!
But it's not just any old 148 lines. By my count it includes the removal of 47 |
To me, the five idlelib changes make the code easier to understand. Nick, please either move them into a separate PR or allow me to do so, and subject to manual testing, I will merge and backport. (I requested this on the PR by maybe you missed it.) If there are other plausible changes in idlelib that you would like considered, make a spinoff issue where we can discuss them first. |
I believe this is complete; feel free to reopen if it is not. |
Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.
Show more details
GitHub fields:
bugs.python.org fields:
The text was updated successfully, but these errors were encountered: