PEP 824: None-coalescing operators - #4799
Conversation
|
@gvanrossum And please can you confirm sponsorship of this as well as #4798? |
Yes of course. |
gvanrossum
left a comment
There was a problem hiding this comment.
Again, I love that these get a serious treatment and I hope we can get the PEP to make it into 3.15. Again I have some editorial suggestions (some of which are generic and could apply to 823 as well) and some grammar nits and typos.
Hnasar
left a comment
There was a problem hiding this comment.
Thanks for writing this up in such a clear way. Exciting to see this move forward!
|
I do think that "coalescing" is a rather off-putting term, and we should come up with a better name for all proposed operations (?., ??, ??=). |
Co-authored-by: Hugo van Kemenade <1324225+hugovk@users.noreply.github.com>
|
Hey @cdce8p! I noticed two small things in the current PEP 824 draft that I would like to inquire about: First, the assignment expression alternative in the "Motivation" section looks like it has its evaluation order backwards: age = (val := user.get_age()) if val is not None else "unknown"Because the condition of a conditional expression is evaluated first, this reads In [15]: age = (val := user.get_age()) if val is not None else "unknown"
---------------------------------------------------------------------------
NameError Traceback (most recent call last)
Cell In[15], line 1
----> 1 age = (val := user.get_age()) if val is not None else "unknown"
NameError: name 'val' is not defined
In [16]: age = val if (val := user.get_age()) is not None else "unknown"
In [17]: age
Out[17]: 20I think the intended version is: age = val if (val := user.get_age()) is not None else "unknown"Second, do you think we could have the specification explicitly describe how get_container()[get_key()] ??= make_default()To me what should happen is _container = get_container()
_key = get_key()
_current = _container[_key]
if _current is None:
_container[_key] = make_default()The reference implementation seems to agree with this, but I think documenting it would be useful, since the current Thanks for your time and looking forward to this PEP helping my code less verbose ;) |
|
Thanks for taking the time to read the draft and provide feedback @gtkacz! If you like a challenge, I've another open PR for the none-aware access operators, feel free to read #4798.
Yes. This must have slipped through at some point.
Your intuition is correct here. Subexpressions on the left hand side are cached. This is actually similar to augmented assignments. My reference implementation for |
|
@cdce8p more than happy to help! Your other PEP 823 will also be an incredible QOL improvement, and I'd love to help with it, but I'm not sure what would you'd want me to do? I'll move the convo there ;) |
gvanrossum
left a comment
There was a problem hiding this comment.
Lots of nits, a few biggies (I don't like the "coalesce" name). Great PEP!
Co-authored-by: Guido van Rossum <gvanrossum@gmail.com>
gvanrossum
left a comment
There was a problem hiding this comment.
Another round. While at a C++ conference I finally found the time to look at these. :-)
Basic requirements (all PEP Types)
pep-NNNN.rst), PR title (PEP 123: <Title of PEP>) andPEPheaderAuthororSponsor, and formally confirmed their approvalAuthor,Status(Draft),TypeandCreatedheaders filled out correctlyPEP-Delegate,Topic,RequiresandReplacesheaders completed if appropriate.github/CODEOWNERSfor the PEPStandards Track requirements
Python-Versionset to valid (pre-beta) future Python version, if relevantDiscussions-ToandPost-History📚 Documentation preview 📚: https://pep-previews--4799.org.readthedocs.build/