Python·100 questions

How to use @dataclass with default_factory?

Answer
Mutable default values cannot be default=[].
Use field(default_factory=list).
Similarly for dict/set.
Prevents shared list across instances.
Common beginner mistake.
Was this answer helpful?

More questions in this topic

Related questions from other topics