[Python-ideas] namedtuple nit...

Nick Coghlan ncoghlan at gmail.com
Sat Jul 29 09:49:35 EDT 2017


On 29 July 2017 at 04:56, Mike Miller <python-ideas at mgmiller.net> wrote:
> Nice.  Ok, so there are different dimensions of mutability.
>
> Still, haven't found any "backdoors" to object(), the one I claimed was
> immutable.

It's possible to write builtin types that are truly immutable, and
there are several examples of that (direct instances of object, tuple
instances, instances of the builtin numeric types), but it isn't
particularly straightforward to make Python defined classes truly
immutable.

While this gets close for stateless instances (akin to instantiating
object() directly):

    >>> class MostlyImmutable:
    ...     __slots__ = ()
    ...     @property
    ...     def __class__(self):
    ...         return type(self)
    ...

It's far more difficult to actually store any meaningful state without
making it open to mutation in some way (since it needs to settable
from __new__, and Python doesn't provide any inherent mechanism from
distinguishing those cases from post-creation modifications).

Cheers,
Nick.

-- 
Nick Coghlan   |   ncoghlan at gmail.com   |   Brisbane, Australia


More information about the Python-ideas mailing list