[Python-Dev] PEP 416: Add a frozendict builtin type

Paul Moore p.f.moore at gmail.com
Thu Mar 1 14:49:41 CET 2012


On 1 March 2012 12:08, Victor Stinner <victor.stinner at gmail.com> wrote:
> New try:
>
> "A frozendict is a read-only mapping: a key cannot be added nor
> removed, and a key is always mapped to the same value. However,
> frozendict values can be mutable (not hashable). A frozendict is
> hashable and so immutable if and only if all values are hashable
> (immutable)."

I'd suggest you don't link immutability and non-hashability so
tightly. Misbehaved objects can be mutable but hashable:

>>> class A:
...   def __init__(self,a):
...     self.a = a
...   def __hash__(self):
...     return 12
...
>>> a = A(1)
>>> hash(a)
12
>>> a.a=19
>>> hash(a)
12

Just avoid using the term "immutable" at all:

"A frozendict is a read-only mapping: a key cannot be added nor
removed, and a key is always mapped to the same value. However,
frozendict values can be mutable. A frozendict is hashable if and
only if all values are hashable."

I realise this is a weaker statement than you'd like to give
(immutability seems to be what people *really* think they want when
they talk about frozen objects), but don't promise immutability if
that's not what you're providing.

More specifically, I'd hate to think that someone for whom security is
an issue would see your original description and think they could use
a frozendict and get safety, only to find their system breached
because of a class like A above. The same could happen to people who
want to handle thread safety via immutable objects, who could also end
up with errors if misbehaving classes found their way into an
application.

Paul.


More information about the Python-Dev mailing list