100%. I don’t understand the obsession with bending dicts into something they are inherently bad at when actual alternatives in dataclasses and Pydantic models right there.
In addition to the already-mentioned suggestions, you can also use marshmallow schemas to serialise complex objects, and there's even a package to autogen marshmallow schemas from dataclasses: https://pypi.org/project/marshmallow-dataclass/
Be aware that pickle is unsafe (as documented near the top of https://docs.python.org/3/library/pickle.html), so prefer other serialization formats except between trusted processes
I can't remember the last time I _wanted_ to pickle something. Parquet and JSON pretty much cover it for me. I guess pickling made more sense before tools like Pydantic were good and popular?
If I want all the python-specific nuance on disk, I know I'm in a bad place!
The problem is that an object can't know whether you're getting a node that you're later going to assign a leaf to... or whether you're getting the node because you want to get the node.
x = dd()
x.a.b.c = "Foo"
is equivalent to:
x = dd()
y = x.a.b
y.c = "Foo"
So "x.a.b" can't raise an exception.
Now, it's python, so you can do unholy things to make pretty much anything work. But I think it would be ugly.
Wow, great job by this "beginner" just "learning a language"! Demonstrates knowledge of so many of Python intricacies that it's worth starting a Non-obfuscated Obfuscated Python Code Contest :)
For anyone who doesn’t watch… the point being made is that this has been done so many times that yet another implementation was pulled from the stdlib in 3.12 (AttrDict).
It’s true. I’ve implemented this several times myself, and thought it was a new/good idea every time until I remembered how many abstractions it breaks if I use it anywhere.
I can’t promise I won’t do it again in a few weeks! `NaturalDictLite` is probably coming around in the accompanying rotation of bad name ideas :p
Run it from a clone of the repo to install it to the system / your user.
I don't see a name to install this from pypi (I see several dotdict libraries on there but they don't link here, so don't know if this is there somewhere).
https://pypi.org/project/python-box/
https://pypi.org/project/munch/