Are instances of user-defined classes mutable?

ZHAOWANCHENG zhaowcheng at 163.com
Thu Aug 6 00:17:17 EDT 2020


the doc of dictionary said "if a tuple contains any mutable object either directly or indirectly, it cannot be used as a key."
i think a instance of user-defined class is mutable, but i found it can be placed into a tuple that as a key of a dict:
    >>> class mycls(object):
    ...     a = 1
    ...
    >>> me = mycls()
    >>> me.a = 2  # mutable?
    >>> {(1, me): 'mycls'}
    {(1, <__main__.mycls object at 0x0000022824DAD668>): 'mycls'}
    >>>


So are instances of user-defined classes mutable or immutable?



More information about the Python-list mailing list