On Wed, Oct 20, 2021 at 3:25 AM Baptiste Carvello
<devel2...@baptiste-carvello.net> wrote:
>
> Le 18/10/2021 à 20:26, Guido van Rossum a écrit :
> >
> > y = None  # Default
> > if config is not None:
> >   handler = config.get("handler")
> >   if handler is not None:
> >     parameters = handler.get("parameters")
> >     if parameters is not None:
> >       y = parameters.get("y")
> >
> > […]
> > Using ?. this can be written as
> >
> > y = config?.get("handler")?.get("parameters")?.get("y")
>
> Sure, but the EAFP version is not that bad:
>
> try:
>   y = config["handler"]["parameters"]["y"]
> except KeyError:
>   y = None
>
> which could be further simplified with an exception-catching expression
> (caveat: keyword usage is pure improvisation, it sounds good, but is
> probably broken :-) :
>
> y = config["handler"]["parameters"]["y"] with KeyError as None
>
> The PEP authors would probably reject this as "hiding errors in code",
> which is true, but none-aware operators also hide errors…

PEP 463 would like to say hi :)

ChrisA
_______________________________________________
Python-Dev mailing list -- python-dev@python.org
To unsubscribe send an email to python-dev-le...@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/M5XHRRAE3CIQACYHU4Q3TBMFXIUYYI3N/
Code of Conduct: http://python.org/psf/codeofconduct/

Reply via email to