On 2021-02-05 22:18, Random832 wrote:
While we're on the subject of assignment expression limitations, I've 
occasionally wanted to write something like

try:
     return a_dict[key]
except KeyError:
     return (a_dict[key] := expression to construct value)

You can already do that with `return a_dict.setdefault(key, your_expression_here)`. If the expression is expensive to evaluate you can use a short-circuiting conditional expression to guard it.

--
Brendan Barnwell
"Do not follow where the path may lead. Go, instead, where there is no path, and leave a trail."
   --author unknown
_______________________________________________
Python-ideas mailing list -- python-ideas@python.org
To unsubscribe send an email to python-ideas-le...@python.org
https://mail.python.org/mailman3/lists/python-ideas.python.org/
Message archived at 
https://mail.python.org/archives/list/python-ideas@python.org/message/5TXK4D5ZGSWKLODJD5NZJDSEOSL2CVMH/
Code of Conduct: http://python.org/psf/codeofconduct/

Reply via email to