-
-
Notifications
You must be signed in to change notification settings - Fork 1.5k
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
PEP 572: Assorted fixes suggested by Rob Cliffe #719
Conversation
pep-0572.rst
Outdated
... | ||
# A loop that can't be trivially rewritten using 2-arg iter() | ||
while (value := read_next_item(file)) is not None: | ||
# Do something with value |
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.
Ick - this example seems wholly contrived. How about keeping the original # A more explicit ...
comment but changing the example to match it?
Before:
for line in iter(somefile.readline, ""):
process(line)
After:
while line := somefile.readline():
process(line)
Maybe that just especially appeals to me because I can never remember what 2-arg iter()
does
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.
The problem with file based examples is that they typically collapse down to for line in somefile: ...
in real code. It really is only in cases where native iteration support isn't available for some reason that you need to resort to the loop-and-a-half pattern instead.
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.
Another real-world example involving files is:
while chunk := somefile.read(8192):
process(chunk)
It doesn't belong to this paragraph though (I'm merely mentioning it)
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.
@giampaolo, I think that would be an excellent example to replace the one here! The clumsy 2-arg iter way of spelling that is
for chunk in iter((lambda: somefile.read(8192)), ""):
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.
Adding this now.
pep-0572.rst
Outdated
@@ -255,7 +262,8 @@ Relative precedence of ``:=`` | |||
|
|||
The ``:=`` operator groups more tightly than a comma in all syntactic | |||
positions where it is legal, but less tightly than all operators, |
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.
s/all operators/all other operators/ ? That change would seem to be in the spirit of the OP's complaints.
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.
According to the table at https://docs.python.org/3/reference/expressions.html#operator-precedence the comma is not an operator in Python. It's always a syntactic construct. I suppose you could argue that it's an operator taking two or more operands, and returns a tuple containing them, but that's pushing the definition a bit.
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.
Right, comma isn't "really" an operator, but pretending otherwise is common in explanatory text. The idea is that the RHS of :=
goes on until you hit a comma or unmatched right paren/bracket/brace. The fiction is useful because specifying the actual grammar is full of details that - while necessary for the implementation - aren't really illuminating.
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.
Okay. Then let's go with "other"; it'll be technically correct in that "all other operators" means "everything except :=
", and colloquially correct in that it means "everything except the comma".
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.
The "all other operators" change is already part of #718.
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.
Added 2 comments, and that's it.
6e6c755
to
e9e14b4
Compare
No description provided.