Revisiting PEP 505

This is definitely a common scenario. I have to deal with similar data structures relatively often. So I can certainly appreciate the desire for having a better way of dealing with such data structures.

What I’m not clear on is why this needs to be syntax. First of all, the only use case I’ve encountered is this sort of unstructured nested dictionary coming from an external system. There’s not a broad spectrum of use cases here, but rather a single, relatively common, one. Secondly, this seems fairly easy to handle using a library. The glom library has already been mentioned here, and it handles this use case just fine. But maybe it’s more than we want here - if so, what would be wrong with a stdlib function[1], something like this?

start_time = traverse(event, "timeRange", "startTime")

The arguments could be strings (representing [KEY]), numbers ([INDEX]), or strings starting with a dot (.KEY). That would seem to cover all the reasonable use cases (I can imagine arguments over the details, but IMO to be convincing they would have to come with a use case that we haven’t seen so far in this discussion).

We could even revisit the question of whether dedicated syntax was worth adding at a later date, once we’d had experience with the library function, and knew what the rough edges were, and what advantages we’d get from adding syntax.


  1. I don’t have a good intuition for where it would go - maybe collections? ↩︎

14 Likes