
This is very reminiscent of the (rejected) PEP 463, Exception-catching expressions (which I still hope will be resurrected some day). It would allow you to write y = (config["handler"]["parameters"]["y"] except KeyError: None) (possibly the parentheses might not be required) which IMO is even better; `except` makes the intent clearer than `with`. Best wishes Rob Cliffe On 20/10/2021 03:05, h.vetinari@gmx.com wrote:
Baptiste Carvello wrote:
y = config["handler"]["parameters"]["y"] with KeyError as None I love the look of this! While it doesn't address everything that PEP505 does, that's IMO a good thing, because - as other people mentioned already - None does too many things already.
On another note, the whole discussion reminds me of wg21.link/p0798, which is about adding something quite similar to C++ (was accepted into C++23), and contains a decent overview of how other languages solve the same thing.
Even though the approach there is probably not applicable (as all python types are implicitly `Optional` in the sense of "can be None", i.e. that API would have to be added all the way down to `object`), it's still ironic that C++'s `and_then` looks more pythonic than what's proposed here. _______________________________________________ Python-Dev mailing list -- python-dev@python.org To unsubscribe send an email to python-dev-leave@python.org https://mail.python.org/mailman3/lists/python-dev.python.org/ Message archived at https://mail.python.org/archives/list/python-dev@python.org/message/6JD5VGTV... Code of Conduct: http://python.org/psf/codeofconduct/