вторник, 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.
_______________________________________________ Python-ideas mailing list Python-ideas@python.org https://mail.python.org/mailman/listinfo/python-ideas Code of Conduct: http://python.org/psf/codeofconduct/