November 13, 2020

How to Group Data Easily With SimpleNamespace

While you could use the usual suspects, like a dictionary, a NamedTuple, a dataclass, or even an “empty” class, there is yet another way: SimpleNamespace. >>> from types import SimpleNamespace >>> simple_ns = SimpleNamespace(a=1, b="two") >>> simple_ns namespace(a=1, b='two') >>> simple_ns.a 1 >>> simple_ns.b 'two' >>> implementation This builtin is implemented in C, but the Python docs show how it would look like in Python: class SimpleNamespace: def __init__(self, /, **kwargs): self....