5 ms·
You can get away with a single hashed dict: from types import SimpleNamespace mydict = { "name": "moe", "age": 3, "test": {"name": "josh"} # wont
by MaxMoney 5y ago
You can get away with a single hashed dict:
from types import SimpleNamespace
mydict = {
"name": "moe",
"age": 3,
"test": {"name": "josh"} # wont work
}
dotted = SimpleNamespace(*mydict)
print(dotted.name, dotted.age) # moe 3
- kkirsche 5y agoIf you don’t mind the heavier weight / newer version requirements, dataclasses from the standard lib also seem to solve this problem.
- whalesalad 5y agonamedtuple is my usual goto for this but problem but I am glad to learn of SimpleNamespace
- youssefabdelm 5y agoExactly my thought...I wonder how the Box library differs.
- MaxMoney 5y agoit probably does nested dicts