# Adding Deep Immutability

**URL:** <https://discuss.python.org/t/adding-deep-immutability/92011>\
**Category:** Ideas\
**Created:** [May 14, 2025, 9:27pm UTC](https://discuss.python.org/t/adding-deep-immutability/92011 "2025-05-14T21:27:34Z")\
**Posts on this page:** 1\
**Showing post:** 92

<div class="post-metadata">

**Author:** ![oscarbenjamin](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/oscarbenjamin/32/1209_2.png) [@oscarbenjamin](https://discuss.python.org/u/oscarbenjamin)\
**Post date:** [May 23, 2025, 1:01pm UTC](https://discuss.python.org/t/adding-deep-immutability/92011/92 "2025-05-23T13:01:37Z")

</div>

> [@mikeshardmind](#):
>
> a function that freezes inputs it doesn’t control is pathological and does not make sense

Agreed but the freeze function is greedy and reaches far from the object that is apparently being frozen. It will easily freeze things that the author does not control unless the object being frozen is very carefully isolated from almost everything in a way that I think many Python programmers will not find easy to understand.

Assuming we can go by the reference implementation I now know the answer to this question:

> [@oscarbenjamin](#):
>
> Is the answer that a Python version of `lru_cache` would be frozen but broken whereas a C version would not be freezable?

The answer is yes. The C version of lru\_cache cannot be frozen but a Python implementation of it would be frozen and would then be broken. Suppose we have:

```python
def cache(func):

    values = {}

    def cached(*key):
        try:
            return values[key]
        except KeyError:
            val = values[key] = func(*key)
            return val

    return cached

@cache
def square(x):
    return x*x

```

Then if you make a class that calls this function you can freeze an instance of your class but it will break the square function:

```python
class A:
    def do_stuff(self, val):
        return square(val)

a = A()

print(square(3)) # works

from immutable import freeze
freeze(a) # seems to work...

print(square(5)) # TypeError: dict is immutable

```

Now by freezing `a` you have broken the `square` function and not just in the sense that it doesn’t work when reached from `a`. The `square` function is broken for all callers including those who had no idea that you were going to freeze anything (e.g. the author of the library in which it is defined).

Touching even so much as a single function from a given library might reach the entire library. Unless every single part of that library is freezable (unlikely for any nontrivial library) you can’t even import from the library for risk that any part of it being in the module globals contaminates everything as unfreezable.

What this means is that you have invisible deeply transitive object colouring. The object graph from freezable objects can never reach anything unfreezable. The runtime cannot reliably detect whether things are freezable though (`square` should not be) and static typing cannot model it either.

---

_[View the full topic](https://discuss.python.org/t/adding-deep-immutability/92011)._
