PEP 814: Add frozendict built-in type

… and …

It’s one thing for keys to disappear from underneath you, but have an object change its type is something else entirely. If you were mutating a list and it suddenly became a tuple in the middle of a function call would you be surprised by that? And don’t forget that dict and frozendict don’t meet until object, so your guards in your code about expecting a dict at the top of your function suddenly turned into a lie.

Remember, this is a built-in type that’s being proposed. That means it will be affecting general Python practices. If we let dicts suddenly change their type in place to become a frozendict we then start encouraging swapping out an object’s type much more often than it is now (which I would argue is extremely rare, and for good reasons). I don’t think these sorts of tricks should be encouraged just to make creating a frozendict instance a bit cheaper.

To be a bit blunt: if there’s a way to make a dict instance a frozendict instance in-place via a method on the object I am -1 on the PEP.

22 Likes