# 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:** 25

<div class="post-metadata">

**Author:** ![blhsing](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/blhsing/32/25812_2.png) [@blhsing](https://discuss.python.org/u/blhsing)\
**Post date:** [May 16, 2025, 1:47am UTC](https://discuss.python.org/t/adding-deep-immutability/92011/25 "2025-05-16T01:47:14Z")

</div>

> [@xFrednet](#):
>
> A less clean solution would be to use a check `isfrozen(self)` in ` __hash__ ()` to dynamically restrict the hash implementation to immutable objects.

This won’t work because a type is determined to be unhashable only if its ` __hash__ ` attribute is `None`. This means that the ` __hash__ ` attribute of an unhashable mutable type can’t be made a dynamic method to make an instance hashable when frozen.

> [@xFrednet](#):
>
> Or maybe have something like ` __frozen_hash__ ()`?

Yes, I think this is the way to go, and to make `isinstance(obj, Hashable)` work for frozen objects we can make changes to `abc` and `collections.abc` such that:

1. `abc.ABCMeta` offers not just a ` __subclasshook__ ` but also an ` __instancehook__ ` that is called by its ` __instancecheck__ ` before falling back to calling ` __subclasscheck__ `.
2. `collections.abc.Hashable` overrides the ` __instancehook__ ` to look for `type(obj). __frozen_hash__ ` if `isfrozen(obj)` is true.

This allows `isinstance(obj, Hashable)` to take the state of the instance itself into account. Without the change it currently can only check its type.

---

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