A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://python.github.io/peps/pep-0416/ below:

PEP 416 – Add a frozendict builtin type

PEP 416 – Add a frozendict builtin type
Author:
Victor Stinner <vstinner at python.org>
Status:
Rejected
Type:
Standards Track
Created:
29-Feb-2012
Python-Version:
3.3
Table of Contents Rejection Notice

I’m rejecting this PEP. A number of reasons (not exhaustive):

On the other hand, exposing the existing read-only dict proxy as a built-in type sounds good to me. (It would need to be changed to allow calling the constructor.) GvR.

Update (2012-04-15): A new MappingProxyType type was added to the types module of Python 3.3.

Abstract

Add a new frozendict builtin type.

Rationale

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 not hashable. A frozendict is hashable if and only if all values are hashable.

Use cases:

Constraints Implementation Recipe: hashable dict

To ensure that a frozendict is hashable, values can be checked before creating the frozendict:

import itertools

def hashabledict(*args, **kw):
    # ensure that all values are hashable
    for key, value in itertools.chain(args, kw.items()):
        if isinstance(value, (int, str, bytes, float, frozenset, complex)):
            # avoid the compute the hash (which may be slow) for builtin
            # types known to be hashable for any value
            continue
        hash(value)
        # don't check the key: frozendict already checks the key
    return frozendict.__new__(cls, *args, **kw)
Objections

namedtuple may fit the requirements 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.

Alternative: dictproxy

Python has a builtin dictproxy type used by type.__dict__ getter descriptor. This type is not public. dictproxy is a read-only view of a dictionary, but it is not read-only mapping. If a dictionary is modified, the dictproxy is also modified.

dictproxy can be used using ctypes and the Python C API, see for example the make dictproxy object via ctypes.pythonapi and type() (Python recipe 576540) by Ikkei Shimomura. The recipe contains a test checking that a dictproxy is “mutable” (modify the dictionary linked to the dictproxy).

However dictproxy can be useful in some cases, where its mutable property is not an issue, to avoid a copy of the dictionary.

Existing implementations

Whitelist approach.

Blacklist approach: inherit from dict and override write methods to raise an exception. It is not truly read-only: it is still possible to call dict methods on such “frozen dictionary” to modify it.

Hashable dict: inherit from dict and just add an __hash__ method.

Links Copyright

This document has been placed in the public domain.


RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4