Are instances of user-defined classes mutable?

Mats Wichmann mats at wichmann.us
Fri Aug 7 10:08:45 EDT 2020


On 8/6/20 8:53 PM, ZHAOWANCHENG wrote:

> So instances of user-defined classes are immutable by default?

No, they're clealry mutable.

> Or the description of "if a tuple contains any mutable object either directly or indirectly, it cannot be used as a key."  in the document is not completely correct?

It's correct, but is not relevant to your case, as you're asking about
an object different than a tuple.

When defining a class, you get by default a hash that is consistent, no
matter what you do to the contents of an instance, so instances are
eligible to be used as dict keys.  You can define the class in a way
that instances cannot be used as keys, by ensuring there is no hash
function.



More information about the Python-list mailing list