Python·100 questions

How to create an immutable object in Python?

Answer
Use dataclass(frozen=True).
Or NamedTuple / typing.NamedTuple.
For collections: tuple instead of list, frozenset instead of set.
Do not mutate internal structures.
For configs, it reduces bugs.
Was this answer helpful?

More questions in this topic

Related questions from other topics