[Python-ideas] Revisiting Immutable Mappings
Zaur Shibzukhov
szport at gmail.com
Tue Oct 16 10:42:55 EDT 2018
вторник, 16 октября 2018 г., 12:29:55 UTC+3 пользователь Steven D'Aprano
написал:
>
>
> > It seems to me that we would need this restriction to make a reasonably
> > universal frozendict that is, itself, hashable.
>
> When people talk about frozendicts being hashable, they mean it in the
> same sense that tuples are hashable.
>
>
> For what it is worth, here's an incomplete, quick and dirty proof of
> concept frozendict, using automatic delegation:
>
> # Not good enough for production, not tested, buyer beware, etc.
> class frozendict:
> def __new__(cls, *args, **kwargs):
> d = dict(*args, **kwargs)
> proxy = types.MappingProxyType(d)
> instance = super().__new__(cls)
> instance.__proxy = proxy
> return instance
> def __hash__(self):
> return hash(tuple(self.items()))
> def __getattr__(self, name):
> return getattr(self.__proxy, name)
> def __getitem__(self, key):
> return self.__proxy[key]
> def __repr__(self):
> return "%s(%r)" % (type(self).__name__, dict(self))
>
> For those who need more performant variant (Cython compiled) of frozendict
frozenmap <https://pypi.org/project/frozenmap/> project can be offer.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20181016/24bafa19/attachment.html>
More information about the Python-ideas
mailing list