immutability is not strictly the same as having an unchangeable value, it is more subtle

Chris Angelico rosuav at gmail.com
Thu Apr 18 04:18:32 EDT 2019


On Thu, Apr 18, 2019 at 6:16 PM Gregory Ewing
<greg.ewing at canterbury.ac.nz> wrote:
>
> Arup Rakshit wrote:
> > What protocols I need to
> > learn, to define a custom immutable class ?
>
> That depends on how strictly you want to enforce immutability.
>
> The easiest thing is not to enforce it at all and simply refrain
> from mutating it. This is very often done.
>
> You can provide some protection against accidental mutation
> by using properties

Another reasonably easy way to make a custom immutable class is to
make use of namedtuple. You can subclass a namedtuple to add methods
to it, for instance.

ChrisA


More information about the Python-list mailing list