On Thu, Oct 11, 2018 at 1:02 PM Cameron Simpson <c...@cskk.id.au> wrote:
>
> On 10Oct2018 20:25, Philip Martin <philip.martin2...@gmail.com> wrote:
> >Steven, that's a great idea, and I would be 100% up for your suggestion to
> >have types.MappingProxyType renamed to frozendict.
>
> I'm not for the rename, myself. Though I'd not be against a frozendict
> factory in builtins, a tiny shim for MappingProxyType.
>
> >However, the differences
> >in the behavior of MappingProxyType's constructor versus dict's would make
> >the API's behavior confusing IMO. For example, MappingProxyType(x=5, y=10)
> >throws a TypeError. I don't think most people would expect this.
>
> Well, if it were called frozendict, indeed not. It should act like dict.
>
> So:
>
>   def frozendict(**kw):
>       return MappingProxyType(kw)
>
> You could make an argument for that (or a slightly heftier version
> accepting the various things dict accepts). Or... you could just keep
> such a thing in your personal kit as a trivial way to spell
> "frozendict". One could argue for the above as a nice example to live in
> the docs perhaps.
>
> But not everything needs a special name.
>

Point of note: A mapping proxy is NOT immutable; it is merely read-only.

>>> from types import MappingProxyType
>>> d = {'a':1, 'b':2}
>>> p = MappingProxyType(d)
>>> p
mappingproxy({'a': 1, 'b': 2})
>>> d['a'] = 3
>>> p
mappingproxy({'a': 3, 'b': 2})

A frozendict type, if it's meant to parallel frozenset, ought to be
hashable (subject to the hashability of its members, of course), but
you can't just take MPT and toss in a __hash__ method. No idea how
important that use-case is, but Michael Selik mentioned "want[ing] to
use a dict as a dict key about once or twice a year", which MPT is not
able to do.

Interestingly, frozenset isn't a subclass of set. I was going to say
that a frozendict ought to be a dict, but maybe that isn't so
important. Which might make a simple pure-Python frozendict actually
pretty easy.

ChrisA
_______________________________________________
Python-ideas mailing list
Python-ideas@python.org
https://mail.python.org/mailman/listinfo/python-ideas
Code of Conduct: http://python.org/psf/codeofconduct/

Reply via email to