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…
Cheers,
Baptiste
_______________________________________________
Python-Dev mailing list -- [email protected]
To unsubscribe send an email to [email protected]
https://mail.python.org/mailman3/lists/python-dev.python.org/
Message archived at
https://mail.python.org/archives/list/[email protected]/message/JWZ2ZG7AIXXWDGNPUZDVLORDFMMEP3XV/
Code of Conduct: http://python.org/psf/codeofconduct/