<div dir="ltr">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.<div><br></div><div><div>from unicodedata import normalize</div><div>prices = [{'croissant': 1}, {'coffee': 3}]</div><div>translation_map = {'apple': 'pomme',  'coffee': 'café'}</div><div>def normalize_text(s):</div><div>    return normalize('NFD', s).encode('ascii', 'ignore').decode("utf-8")</div><div><br></div><div>def usd_to_eur(v):</div><div>    return v / 1.2</div><div><br></div><div>def passthrough(v):</div><div>    return v</div><div><br></div><div>def convert_price(record, convert_map=translation_map):</div><div>    # remove accents for price mapping. Oops!</div><div>    for key, value in convert_map.items():</div><div>        convert_map[key] = normalize_text(value)</div><div><br></div><div>    record = {</div><div>        convert_map[k]: usd_to_eur(v) for k, v in record.items()</div><div>    }</div><div>    return record</div></div></div><br><div class="gmail_quote"><div dir="ltr">On Wed, Oct 10, 2018 at 6:24 PM Steven D'Aprano <<a href="mailto:steve@pearwood.info">steve@pearwood.info</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">Hi Philiip, and welcome,<br>
<br>
On Wed, Oct 10, 2018 at 12:04:48PM -0500, Philip Martin wrote:<br>
<br>
> I generally have used MappingProxyType as a way to set default mapping <br>
> to a function or to set an empty mapping to a function.<br>
<br>
> I've created a gist with an example use case:<br>
> <br>
> <a href="https://gist.github.com/pmart123/493edf84d9aa61691ca7321325ebb6ab" rel="noreferrer" target="_blank">https://gist.github.com/pmart123/493edf84d9aa61691ca7321325ebb6ab</a><br>
<br>
Please try to keep the discussion in one place (i.e. here), for the <br>
benefit of the archives and for those who have email access but not <br>
unrestricted web access.<br>
<br>
Can you explain (in English) your use-case, and why MappingProxyType <br>
isn't suitable? If it *is* suitable, how does your proposal differ?<br>
<br>
If the only proposal is to rename types.MappingProxyType to a builtin <br>
"frozendict", that's one thing; if the proposal is something else, you <br>
should explain what.<br>
<br>
<br>
> I've included an example of what code typically looks like when using<br>
> MappingProxyType and what it could look like with a<br>
> frozendict implementation.<br>
<br>
Wouldn't that be just:<br>
<br>
    from types import MappingProxyType as frozendict<br>
    d = frozendict({'spam': 1, 'eggs': 2})<br>
<br>
versus:<br>
<br>
    d = frozendict({'spam': 1, 'eggs': 2})<br>
<br>
Apart from the initial import, how would they be different? You want a <br>
frozendict; the existing MappingProxyType provides a frozendict (with a <br>
surprising name, but never mind...). Wouldn't you use them exactly the <br>
same way? They both (I presume) offer precisely the same read-only <br>
access to the mapping interface.<br>
<br>
<br>
<br>
-- <br>
Steve<br>
_______________________________________________<br>
Python-ideas mailing list<br>
<a href="mailto:Python-ideas@python.org" target="_blank">Python-ideas@python.org</a><br>
<a href="https://mail.python.org/mailman/listinfo/python-ideas" rel="noreferrer" target="_blank">https://mail.python.org/mailman/listinfo/python-ideas</a><br>
Code of Conduct: <a href="http://python.org/psf/codeofconduct/" rel="noreferrer" target="_blank">http://python.org/psf/codeofconduct/</a><br>
</blockquote></div>