Thanks for the suggestion! I agree that using a list is clearer that having a trailing comma, I like it!
I still think that having a one() function would be useful, since: 1. I think it spells the intention more clearly. Also the exception would be easier to understand, since errors in tuple unpacking usually mean something else. 2. The one() function allows you to use the result inside an expression without assigning it to a variable.
On Mon, Jul 27, 2020 at 10:30 PM Chris Angelico firstname.lastname@example.org wrote:
On Tue, Jul 28, 2020 at 5:24 AM Steven Barker email@example.com wrote:
A single-name unpacking assignment can do exactly what you want, albeit
with slightly less helpful exception messages:
jack, = (p for p in people if p.id == '1234') # note comma after
the name jack
Agreed. As a minor readability refinement, I would prefer to spell this with square brackets:
[jack] = (p for p in people if p.id == '1234')
The effect is identical, but for the one-item unpack, I prefer not to have the vital-but-subtle trailing comma.
(You can use square brackets on larger assignment lists too, but they don't add anything.)
ChrisA _______________________________________________ Python-Dev mailing list -- firstname.lastname@example.org To unsubscribe send an email to email@example.com https://mail.python.org/mailman3/lists/python-dev.python.org/ Message archived at https://firstname.lastname@example.org/message/62ZAKSHT... Code of Conduct: http://python.org/psf/codeofconduct/