<div dir="ltr"><div dir="ltr"><div>I think it could inherit from the Mapping abc?</div><div><br></div><div>class frozendict(Mapping):</div><div>    def __new__(cls, *args, **kwargs):</div><div>        d = dict(*args, **kwargs)</div><div>        proxy = MappingProxyType(d)</div><div>        instance = super().__new__(cls)</div><div>        instance.__proxy = proxy</div><div>        </div><div>        return instance</div><div><br></div><div>    def __hash__(self):</div><div>        return hash(tuple(self.items()))</div><div>    </div><div>    def __getattr__(self, name):</div><div>        return getattr(self.__proxy, name)</div><div><br></div><div>    def __getitem__(self, key):</div><div>        return self.__proxy[key]</div><div><br></div><div>    def __iter__(self):</div><div>        return self.__proxy.__iter__()</div><div><br></div><div>    def __len__(self):</div><div>        return len(self.__proxy)</div><div><br></div><div>    def __repr__(self):</div><div>        return "%s(%r)" % (type(self).__name__, dict(self))</div><div><br></div></div></div><br><div class="gmail_quote"><div dir="ltr">On Tue, Oct 16, 2018 at 4:29 AM 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">On Tue, Oct 16, 2018 at 01:02:03AM -0700, George Leslie-Waksman wrote:<br>
> Would a frozendict require that keys and values be hashable?<br>
<br>
Keys, yes. Values, no.<br>
<br>
If the values were hashable, the frozendict itself would also be <br>
hashable. If not, then it would be like trying to hash a tuple with <br>
unhashable items:<br>
<br>
py> hash((1, 2, {}, 3))<br>
Traceback (most recent call last):<br>
  File "<stdin>", line 1, in <module><br>
TypeError: unhashable type: 'dict'<br>
<br>
<br>
> It seems to me that we would need this restriction to make a reasonably<br>
> universal frozendict that is, itself, hashable.<br>
<br>
When people talk about frozendicts being hashable, they mean it in the <br>
same sense that tuples are hashable. <br>
<br>
<br>
For what it is worth, here's an incomplete, quick and dirty proof of <br>
concept frozendict, using automatic delegation:<br>
<br>
# Not good enough for production, not tested, buyer beware, etc.<br>
class frozendict:<br>
    def __new__(cls, *args, **kwargs):<br>
        d = dict(*args, **kwargs)<br>
        proxy = types.MappingProxyType(d)<br>
        instance = super().__new__(cls)<br>
        instance.__proxy = proxy<br>
        return instance<br>
    def __hash__(self):<br>
        return hash(tuple(self.items()))<br>
    def __getattr__(self, name):<br>
        return getattr(self.__proxy, name)<br>
    def __getitem__(self, key):<br>
        return self.__proxy[key]<br>
    def __repr__(self):<br>
        return "%s(%r)" % (type(self).__name__, dict(self))<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>