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

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

Merged

Conversation

@gvanrossum
Copy link
Member

commented Apr 24, 2019

This is a stub, others should elaborate. @emilyemorehouse

https://bugs.python.org/issue35224

@tirkarthi
Copy link
Contributor

left a comment

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

This comment has been minimized.

Copy link
Member Author

commented Apr 24, 2019

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

This comment has been minimized.

Copy link
Contributor

commented Apr 24, 2019

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:

This comment has been minimized.

Copy link
@serhiy-storchaka

serhiy-storchaka Apr 24, 2019

Member

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

This comment has been minimized.

Copy link
@gvanrossum

gvanrossum Apr 24, 2019

Author Member

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 come 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

This comment has been minimized.

Copy link
Member Author

commented Apr 24, 2019

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

@gvanrossum gvanrossum merged commit 09d434c into python:master Apr 24, 2019

4 checks passed

bedevere/issue-number Issue number 35224 found
Details
bedevere/news "skip news" label found
continuous-integration/appveyor/pr AppVeyor build succeeded
Details
continuous-integration/travis-ci/pr The Travis CI build passed
Details
@bedevere-bot

This comment has been minimized.

Copy link

commented Apr 24, 2019

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

@gvanrossum gvanrossum deleted the gvanrossum:whats-new-assignment-expressions branch Apr 24, 2019

arnolddumas added a commit to arnolddumas/cpython that referenced this pull request May 3, 2019
Issue 35224: Add PEP 572 (assignment expressions) to What's New (pyth…
…on#12941)

This is meant as a stub, during the PyCon sprints we can iterate.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
5 participants
You can’t perform that action at this time.