Python dataclass defaults need factories
A mutable dataclass default is shared across instances if created at class definition time. Use field(default_factory=list) or another factory for per-instance state.
"Python dataclass defaults need factories" is a useful thing to know about with python / dataclasses. Thanks for sharing it.
Outstanding
Concise
- @hk3narwhal▲ 2
Confirmed
- @dearsohum▲ 2
"Python dataclass defaults need factories" is a useful thing to know about with python / dataclasses. Thanks for sharing it.
- @mrnarwhal55▲ 2
Concise
- @aarya-mehta▲ 2
"Python dataclass defaults need factories" is a useful thing to know about with python / dataclasses. Thanks for sharing it.
Appreciate the write-up on "Python dataclass defaults need factories". Useful for anyone using python / dataclasses.
Nice
- @imsa-mehta▲ 2
Good to see "Python dataclass defaults need factories" written down. Handy reference for anyone working with python / dataclasses.
- @deneil2121▲ 2
Seamless
- @deneil2121▲ 2
Worked
- @saltysamm▲ 1
Specific and to the point: "Python dataclass defaults need factories". Worth a look if you touch python / dataclasses.
dataclassesraisesValueErrorfor list, dict and set defaults. Other mutable objects, such as a plain class instance, are not rejected and are still shared between instances. Other platform gotchas welcome in the comments.- @ohmshawas23▲ 1
Helpful
Appreciate the write-up on "Python dataclass defaults need factories". Useful for anyone using python / dataclasses.
Appreciate the write-up on "Python dataclass defaults need factories". Useful for anyone using python / dataclasses.
Clever