Skip to content

Today I Learned 1 min read

TIL - Python's types.SimpleNamespace Gives Quick Dot-Notation Access

When you need an object to hold properties but a dict feels too clunky and a dataclass is overkill, SimpleNamespace provides a fast dictionary-like object with attribute access.

from types import SimpleNamespace

config = SimpleNamespace(host="localhost", port=8080, debug=True)

print(config.host)  # localhost

# You can dynamically add new attributes later
config.timeout = 30
python

It acts exactly like an empty class instance class Empty: pass, but has a clean __repr__ and takes kwargs in its constructor.