8000 bpo-35224: Add PEP 572 (assignment expressions) to What's New by gvanrossum · Pull Request #12941 · python/cpython · GitHub
[go: up one dir, main page]

Skip to content

bpo-35224: Add PEP 572 (assignment expressions) to What's New #12941

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

Merged

Conversation

gvanrossum
Copy link
Member
@gvanrossum gvanrossum commented Apr 24, 2019

This is a stub, others should elaborate. @emilyemorehouse

https://bugs.python.org/issue35224

Copy link
Member
@tirkarthi tirkarthi left a comment

Choose a reason for hiding this comment

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

It will be great to have this entry since the last alpha for 3.8 is by Monday.

Personal preference : It would be good to have an example with less nesting of parens.

if name := person.get("name"):
    print(f"Hello, {name}")

@gvanrossum
Copy link
Member Author

It will be great to have this entry since the last alpha for 3.8 is by Monday.

I'll try to land it ASAP.

Personal preference : It would be good to have an example with less nesting of parens.

if name := person.get("name"):
    print(f"Hello, {name}")

Hm, I prefer to use parentheses. In practice most examples need them anyway. Also we heard on the bpo issue that people try a := 1 at the REPL prompt and find it doesn't work, then assume it's not implemented yet. An example with parentheses at least gives a little hint towards that.

@tirkarthi
Copy link
Member

Hm, I prefer to use parentheses. In practice most examples need them anyway. Also we heard on the bpo issue that people try a := 1 at the REPL prompt and find it doesn't work, then assume it's not implemented yet. An example with parentheses at least gives a little hint towards that.

Makes sense. Thanks 👍

There is new syntax (the "walrus operator", ``:=``) to assign values
to variables as part of an expression. Example::

if (n := len(a)) > 10:
Copy link
Member

Choose a reason for hiding this comment

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

Is this the recommended way of writing such code? Is it more preferable than a separate statement n = len(a)?

Copy link
Member Author

Choose a reason for hiding this comment

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

Writing it this way indicates to the human reader (though not to the interpreter -- see the PEP's fine print about scopes :-) that n is only relevant inside the if block.

You are really free to write it either way, there is no recommendation intended -- this is just a quick example. (I strongly advise everyone to try and c 8000 ome up with examples that are meaningful, rather than using foo, bar, spam and ham. This one was a compromise. Perhaps someone can come up with a more realistic example where data over (or under!) a certain length must be rejected.

@gvanrossum
Copy link
Member Author

I'm landing this now. @emilyemorehouse please forgive me. See you at PyCon!

@gvanrossum gvanrossum merged commit 09d434c into python:master Apr 24, 2019
@bedevere-bot
Copy link

@gvanrossum: Please replace # with GH- in the commit message next time. Thanks!

@gvanrossum gvanrossum deleted the whats-new-assignment-expressions branch April 24, 2019 18:30
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
docs Documentation in the Doc dir skip news
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants
0