On Mon, 18 Oct 2021 at 19:29, Guido van Rossum <guido@python.org> wrote:
> 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")
For this particular usage, I'd much rather have a functional API, like
y = get_config(config, "handler", "parameters", "y")
I agree with Paul here... and am pretty sure I did the last time this went around. Whenever the issue comes up, it's about JSON. Or *maybe* about something very similar that goes by another name or format details.
And there already exists a pretty good, pretty standard, approach called JSONPath that deals with exactly this kind of thing. This, in turn, is largely the same as XPath which serves the same role for XML documents.
I don't think it necessarily needs to be in the standard library, but the mini-language for extracting data from trees with frequently missing branches can very well simply be a mini-language. It'll wind up a lot like XPath/JSONPath, but something a little bit different could be good too.
--