3 ms·
Your version is mutable, whereas a namedtuple is immutable. a very important difference for my field. I use namedtuples all the time. Also, I can't expand an i
by jquast 9y ago
Your version is mutable, whereas a namedtuple is immutable. a very important difference for my field. I use namedtuples all the time.
Also, I can't expand an instance of your Point3d as arguments to a function, DrawPoint(*p), I would have to write DrawPoint(p.x, p.y, p.z), did I preserve order correctly?
Whereas a namedtuple expands and preserves order.
- IgorPartola 9y agoWouldn't it be so convenient if Python provided a way to define immutable classes? Also, you can always provide a `draw_point(*p.args)` which isn't bad at all.
- yes_or_gnome 9y agoYou can get close. Set __slots__ and then define each as a property with no setter and no deleter. However, an object can still be modified if it stores a mutable object (list, dict) and, while I'm not in front of a computer atm,I believe you can overwrite __slots__ to do whatever. Here's an SO answer that explains more than I'll ever know about slots. https://stackoverflow.com/questions/472000/usage-of-slots https://stackoverflow.com/questions/472000/usage-of-slots
- IgorPartola 9y agoRe: storing mutable objects, yea but tuples do that too: t = ({}, 1) t[0]['foo'] = 'bar'