Re: [Python-Dev] Add a frozendict builtin type

2012年3月01日 05:16:46 -0800

> 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
_______________________________________________
Python-Dev mailing list
[email protected]
http://mail.python.org/mailman/listinfo/python-dev
Unsubscribe: 
http://mail.python.org/mailman/options/python-dev/archive%40mail-archive.com

Reply via email to