Short Tutorial on Python's ChainMap
pythonsbyte.substack.com
pythonsbyte.substack.com
I do feel like the article lacks depth in describing what a chainmap actually is, how I can be sure which key takes precedence and lastly, I am not quite sure if I am convinced if the method for sanity checking values scales very well.
I've been using it a bit for tree structures where an element could be a tree or a leaf, so isintance(node, tuple) is just the ticket. Seems simpler than creating a Node class, and can easily be dumped to json where I need
What do you think about this approach?
(That is kind of why I named the newsletter as `Python's Byte` :) )
Anyway, I enjoyed this article mostly because it jumped right into what it was for. It often takes articles just like this one to alert folks like me to one of the more interesting tools the language has to offer. I happen to have a number of projects I have to make changes to that might benefit from this structure. So kudos!
TLDR from the docs: ChainMap groups multiple dicts or other mappings together to create a single, updateable view.
This is the important detail. This is much faster than how you'd solve it pre-3.3.
I found myself wondering why this required a new class in the stdlib until that line.
> Be kind.