How to create an object from a dictionary with dot access — without classes and dataclasses?
When you're working with JSON, configurations, or APIs, constant access via dict['key'] clutters the code and worsens readability:
SimpleNamespace gives the same result, but with dot access:
In this case, the object remains dynamic, and you can add fields:
However, the keys must be valid attribute names, and this only works for flat dictionaries (nesting is not converted).
🔥Convenient for prototyping, testing, and simple data.
#Python #DataStructures #SimpleNamespace #CodingTips #DevTools #Programming
✨ Join Best TG Channels https://t.iss.one/addlist/0f6vfFbEMdAwODBk
⭐️ Join Our WhatsApp Channel https://whatsapp.com/channel/0029VaC7Weq29753hpcggW2A
🚀 Level up your AI & Data Science skills with HelloEncyclo — a growing all-in-one platform featuring hands-on courses in LLMs, Deep Learning, MLOps, Data Engineering, and more.
✅ 13 courses live + 40+ coming soon
🎯 One access, lifetime updates
🔑 Use code: PRESALE-BOOK-WAVE-2GFG
👉 https://helloencyclo.com/?ref=HUSSEINSHEIKHO
When you're working with JSON, configurations, or APIs, constant access via dict['key'] clutters the code and worsens readability:
data = {"host": "localhost", "port": 5432}
data["host"]SimpleNamespace gives the same result, but with dot access:
cfg = SimpleNamespace(**data)
print(cfg.host)
In this case, the object remains dynamic, and you can add fields:
cfg.debug = True
However, the keys must be valid attribute names, and this only works for flat dictionaries (nesting is not converted).
🔥Convenient for prototyping, testing, and simple data.
#Python #DataStructures #SimpleNamespace #CodingTips #DevTools #Programming
✨ Join Best TG Channels https://t.iss.one/addlist/0f6vfFbEMdAwODBk
⭐️ Join Our WhatsApp Channel https://whatsapp.com/channel/0029VaC7Weq29753hpcggW2A
🚀 Level up your AI & Data Science skills with HelloEncyclo — a growing all-in-one platform featuring hands-on courses in LLMs, Deep Learning, MLOps, Data Engineering, and more.
✅ 13 courses live + 40+ coming soon
🎯 One access, lifetime updates
🔑 Use code: PRESALE-BOOK-WAVE-2GFG
👉 https://helloencyclo.com/?ref=HUSSEINSHEIKHO
❤2