[Python-Dev] Add a frozendict builtin type

Victor Stinner victor.stinner at gmail.com
Thu Mar 1 14:07:10 CET 2012


> Here are my real-world use cases. Not for security, but for safety and
> performance reasons (I've built by own RODict and ROList modeled after
> dictproxy):
>
> - Global, but immutable containers, e.g. as class members

I attached type_final.patch to the issue #14162 to demonstrate how
frozendict can be used to implement a "read-only" type. Last version:
http://bugs.python.org/file24696/type_final.patch

Example:

>>> class FinalizedType:
...   __final__=True
...   attr = 10
...   def hello(self):
...     print("hello")
...
>>> FinalizedType.attr=12
TypeError: 'frozendict' object does not support item assignment
>>> FinalizedType.hello=print
TypeError: 'frozendict' object does not support item assignment

(instance do still have a mutable dict)

My patch checks for the __final__ class attribute, but the conversion
from dict to frozendict may be done by a function or a type method.
Creating a read-only type is a different issue, it's just another
example of frozendict usage.

Victor


More information about the Python-Dev mailing list