
On 4/14/2020 10:09 PM, Cameron Simpson wrote:
On 14Apr2020 21:25, Guido van Rossum <guido@python.org> wrote:
On Tue, Apr 14, 2020 at 9:08 PM Raymond Hettinger < raymond.hettinger@gmail.com> wrote:
[GvR]
We should not try to import JavaScript's object model into Python.
Yes, I get that. Just want to point-out that working with heavily nested dictionaries (typical for JSON) is no fun with square brackets and quotation marks.
Yeah, I get that too. So maybe this should be limited to JSON? Could the stdlib json library be made to return objects that support this (maybe with an option)?
I find this feels slightly special purpose. Though admirably restrained. I think I dislike the need to detour thorough the json module to get such a feature.
Like many others, I recently implemented one of these __getattr__+__getitem__ SimpleNamespaces. I'm hacking on some mappings which map dotted-names to values. So the natural implementation is dicts or dict subclasses. But I'm also feeding these to format strings, so I want to write:
"Hi {dotted.name}"
so I've made a nesting of SimpleNamespace so that I can use nice dotted a.b.c.d type names in the format string. And because I'm using .format_map, the top level namespace also supports __getitem__.
Now, my dict subclass has a .ns() method returning one of the above SimpleNamespace subclasses, but I can readily imagine a utility function in collection that made such a thing.
Restricting that to only work via some contrived path through the JSON module seems... clunky.
Cheers, Cameron Simpson <cs@cskk.id.au>
Interesting comments. So .ns allows you to convert a nested dict to a dotted one "on demand" but the initial conversion only needs to do the top level object? That sounds similar to the glom that Nathaniel brought to the attention of this thread, which I hadn't found before. But glom requires bulkier syntax. When I read his example, I thought to myself "What if one could write his 'glom(json_obj, "a.b.c")' example as json_obj['a.b.c'] or better as non_json_obj.a.b.c ? I can imagine an implementation of my first "what-if" fully in the top-level object, but not of the second (but my internals imagination is probably limited). Indeed, many of my use-cases are non-json, although a bunch are json also.