Skip to content
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

Merged
merged 2 commits into from
Jul 10, 2018

Conversation

gvanrossum
Copy link
Member

No description provided.

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
Copy link
Member

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 ☹️

Copy link
Contributor

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.

Copy link
Contributor

@giampaolo giampaolo Jul 10, 2018

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)

Copy link
Member

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)), ""):

Copy link
Member Author

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,
Copy link
Member

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.

Copy link
Contributor

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.

Copy link
Member

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.

Copy link
Contributor

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".

Copy link
Member Author

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.

Copy link
Member

@tim-one tim-one left a 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.

@gvanrossum gvanrossum merged commit e57063c into master Jul 10, 2018
@gvanrossum gvanrossum deleted the pep-572-rob-cliffe-comments branch July 10, 2018 18:19
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants