Op 12/04/2023 om 6:58 schreef dn via Python-list:
Are dataclasses (or instances thereof) mutable or immutable?
- and in what sense?
Instances of dataclasses are mutable, just like normal classes. Dataclasses *are* normal classes, with some extra special methods. They are totally different from namedtuples, even though the use cases somewhat overlap. They *can* be immutable, I think, if the programmer takes care to make them so. I don't think adding __hash__() is enough: as I understand it's an indication that a class is immutable, but doesn't actually by itself make it so. "Mutability is a complicated property that depends on the programmer’s intent, the existence and behavior of |__eq__()|, and the values of the |eq| and |frozen| flags in the |dataclass()| <https://docs.python.org/3.10/library/dataclasses.html#dataclasses.dataclass> decorator.", says the documentation.
Amongst the four benefits ChatGPT listed was:
«
Immutable instances: By default, instances of dataclasses are immutable, which means that once created, their attributes cannot be modified. This can help to prevent unintended modifications to the data.
»

Huh? If we'd been discussing namedtuples over (say) dictionaries, I'd perhaps have accepted the reply.
ChatGPT is wrong.
Anything I've 'missed'?
- or a salutary tale of not depending upon ChatGPT etc?
You didn't miss anything, ChatGPT is wrong. The thing to look out for is that when ChatGPT is wrong, it sounds just as convincing as when it's right; there is no indication in it's tone or style that it's making things up. Always double check!

--
"A common mistake that people make when trying to design something completely
foolproof is to underestimate the ingenuity of complete fools."
        -- Douglas Adams
--
https://mail.python.org/mailman/listinfo/python-list

Reply via email to