3 ms·
The Python baseline is that a class does not know, and doesn't pretend to know, what attributes it could contain, everything is welcome unless extra code is add
by HelloNurse 4y ago
The Python baseline is that a class does not know, and doesn't pretend to know, what attributes it could contain, everything is welcome unless extra code is added to repress rogue assignments.
Trivial getters and setters, __init__ methods that copy their parameters to members of the same name, and other boring Java-like patterns are certainly served well by convenience tools like the attrs library or dataclasses, but distinguishing records from serious classes requires significant skill.
- eru 4y ago> [...], everything is welcome unless extra code is added to repress rogue assignments. I think slots do that, don't they?
- HelloNurse 4y agoYes, and dataclasses offer an even nicer notation.
- kzrdude 4y agoDataclasses make it very easy to define such classes indeed but they don't disallow extra attributes. So they really compose with a lot of other techniques
- packetlost 4y agoI'm not sure what you mean here, GP was saying dataclasses themselves don't prevent rogue assignments, but they have a nicer syntax for generating a `__slots__` automatically based on type-hints/class attributes which absolutely does.
- kzrdude 4y agoI see, new in Py 3.10 and new to me