On Tue, Mar 25, 2014 at 7:36 AM, Steven D'Aprano <steve+comp.lang.pyt...@pearwood.info> wrote: > Yes, Python could have changed the meaning of {} to mean the empty set. > But you know what? The empty set is not that important. Sets are not > fundamental to Python. Python didn't even have sets until 2.3, and at > first they were just a standard library module, not even built-in. Dicts, > on the other hand, are fundamental to Python. They are used everywhere. > Python is, in a very real sense, built on dicts, not sets. You can > implement sets starting from dicts, but not the other way around: dicts > are more fundamental than sets.
Challenge accepted! The _lookup method in the following is based on a recipe from Raymond Hettinger. from collections.abc import MutableMapping class SetBasedDict(MutableMapping): def __init__(self, initial, **kwargs): self._contents = set() self.update(initial, **kwargs) def clear(self): self._contents.clear() def __iter__(self): for item in self._contents: yield item.key def __len__(self): return len(self._contents) def __getitem__(self, key): item = self._lookup(key) if item is None: raise KeyError(key) return item.value def __setitem__(self, key, value): item = self._lookup(key) if item is not None: item.value = value else: item = _DictItem(key, value) self._contents.add(item) def __delitem__(self, key): self._contents.remove(_DictItem(key, None)) def _lookup(self, key): p = _DictSearchProxy(key) if p in self._contents: return p.match return None class _DictItem: def __init__(self, key, value): self.key = key self.value = value def __hash__(self): return hash(self.key) def __eq__(self, other): if not isinstance(other, _DictItem): return NotImplemented return self.key == other.key class _DictSearchProxy: def __init__(self, obj): self.obj = obj self.match = obj def __eq__(self, other): if not isinstance(other, _DictItem): return NotImplemented result = (self.obj == other.key) if result: self.match = other return result def __hash__(self): return hash(self.obj) -- https://mail.python.org/mailman/listinfo/python-list