[Python-Dev] PEP 416: Add a frozendict builtin type (original) (raw)

Victor Stinner victor.stinner at haypocalc.com
Wed Feb 29 19:21:37 CET 2012


As requested, I create a PEP and a related issue:

http://www.python.org/dev/peps/pep-0416/ http://bugs.python.org/issue14162

The PEP 416 is different from my previous propositions: frozendict values can be mutable and dict doesn't inherit from frozendict anymore. But it is still possible to use the PyDict C API on frozendict (which is more an implementation detail).

TODO:

--

PEP: 416 Title: Add a frozendict builtin type Version: RevisionRevisionRevision Last-Modified: DateDateDate Author: Victor Stinner <victor.stinner at gmail.com> Status: Draft Type: Standards Track Content-Type: text/x-rst Created: 29-February-2012 Python-Version: 3.3

Abstract

Add a new frozendict builtin type.

Rationale

A frozendict mapping cannot be changed, but its values can be mutable (not hashable). A frozendict is hashable and so immutable if all values are hashable (immutable).

Use cases of frozendict:

Constraints

Implementation

Recipe: immutable dict

An immutable mapping can be implemented using frozendict::

import itertools

class immutabledict(frozendict):
    def __new__(cls, *args, **kw):
        # ensure that all values are immutable
        for key, value in itertools.chain(args, kw.items()):
            if not isinstance(value, (int, float, complex, str, bytes)):
                hash(value)
        # frozendict ensures that all keys are immutable
        return frozendict.__new__(cls, *args, **kw)

    def __repr__(self):
        return 'immutabledict' + frozendict.__repr__(self)[10:]

Objections

namedtuple may fit the requiements of a frozendict.

A namedtuple is not a mapping, it does not implement the Mapping abstract base class.

frozendict can be implemented in Python using descriptors" and "frozendict just need to be practically constant.

If frozendict is used to harden Python (security purpose), it must be implemented in C. A type implemented in C is also faster.

The PEP 351 was rejected.

The PEP 351 tries to freeze an object and so may convert a mutable object to an immutable object (using a different type). frozendict doesn't convert anything: hash(frozendict) raises a TypeError if a value is not hashable. Freezing an object is not the purpose of this PEP.

Links

Copyright

This document has been placed in the public domain.



More information about the Python-Dev mailing list