[Python-ideas] Revisiting Immutable Mappings

Philip Martin philip.martin2007 at gmail.com
Wed Oct 10 21:26:37 EDT 2018


It would help over using a regular dict as a default argument to a function
by preventing accidental mutation of the default or constant mapping. This
is a quickly contrived example of the convert_price function now having a
side effect by changing the translation_map.

from unicodedata import normalize
prices = [{'croissant': 1}, {'coffee': 3}]
translation_map = {'apple': 'pomme',  'coffee': 'café'}
def normalize_text(s):
    return normalize('NFD', s).encode('ascii', 'ignore').decode("utf-8")

def usd_to_eur(v):
    return v / 1.2

def passthrough(v):
    return v

def convert_price(record, convert_map=translation_map):
    # remove accents for price mapping. Oops!
    for key, value in convert_map.items():
        convert_map[key] = normalize_text(value)

    record = {
        convert_map[k]: usd_to_eur(v) for k, v in record.items()
    }
    return record

On Wed, Oct 10, 2018 at 6:24 PM Steven D'Aprano <steve at pearwood.info> wrote:

> Hi Philiip, and welcome,
>
> On Wed, Oct 10, 2018 at 12:04:48PM -0500, Philip Martin wrote:
>
> > I generally have used MappingProxyType as a way to set default mapping
> > to a function or to set an empty mapping to a function.
>
> > I've created a gist with an example use case:
> >
> > https://gist.github.com/pmart123/493edf84d9aa61691ca7321325ebb6ab
>
> Please try to keep the discussion in one place (i.e. here), for the
> benefit of the archives and for those who have email access but not
> unrestricted web access.
>
> Can you explain (in English) your use-case, and why MappingProxyType
> isn't suitable? If it *is* suitable, how does your proposal differ?
>
> If the only proposal is to rename types.MappingProxyType to a builtin
> "frozendict", that's one thing; if the proposal is something else, you
> should explain what.
>
>
> > I've included an example of what code typically looks like when using
> > MappingProxyType and what it could look like with a
> > frozendict implementation.
>
> Wouldn't that be just:
>
>     from types import MappingProxyType as frozendict
>     d = frozendict({'spam': 1, 'eggs': 2})
>
> versus:
>
>     d = frozendict({'spam': 1, 'eggs': 2})
>
> Apart from the initial import, how would they be different? You want a
> frozendict; the existing MappingProxyType provides a frozendict (with a
> surprising name, but never mind...). Wouldn't you use them exactly the
> same way? They both (I presume) offer precisely the same read-only
> access to the mapping interface.
>
>
>
> --
> Steve
> _______________________________________________
> Python-ideas mailing list
> Python-ideas at python.org
> https://mail.python.org/mailman/listinfo/python-ideas
> Code of Conduct: http://python.org/psf/codeofconduct/
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20181010/5a0e4701/attachment.html>


More information about the Python-ideas mailing list