Path: csiph.com!newsfeed.hal-mli.net!feeder3.hal-mli.net!newsfeed.hal-mli.net!feeder1.hal-mli.net!newsfeed.xs4all.nl!newsfeed3a.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.002 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'python.': 0.02; 'else:': 0.03; 'subject:Python': 0.06; 'none:': 0.07; '2.3,': 0.09; 'obj': 0.09; 'subject:language': 0.09; 'python': 0.11; 'def': 0.12; '**kwargs)': 0.16; '**kwargs):': 0.16; 'clear(self):': 0.16; 'key):': 0.16; 'recipe': 0.16; 'self.obj': 0.16; 'sense,': 0.16; 'set()': 0.16; 'subject: \n ': 0.16; 'subject:?)': 0.16; 'subject:unicode': 0.16; 'wrote:': 0.18; 'library': 0.18; 'import': 0.22; 'module,': 0.24; 'skip:_ 20': 0.27; 'header:In- Reply-To:1': 0.27; 'am,': 0.29; 'raise': 0.29; 'important.': 0.30; 'raymond': 0.30; 'sets': 0.30; 'message-id:@mail.gmail.com': 0.30; '25,': 0.31; "d'aprano": 0.31; 'sets.': 0.31; 'steven': 0.31; 'class': 0.32; 'skip:_ 10': 0.34; 'could': 0.34; 'skip:s 30': 0.35; 'but': 0.35; 'received:google.com': 0.35; 'set.': 0.36; 'yield': 0.36; "didn't": 0.36; 'method': 0.36; 'starting': 0.37; 'implement': 0.38; 'to:addr:python-list': 0.38; 'to:addr:python.org': 0.39; 'changed': 0.39; 'even': 0.60; 'challenge': 0.61; 'first': 0.61; 'real': 0.63; 'skip:n 10': 0.64; 'more': 0.64; 'mar': 0.68; 'subject:this': 0.83; 'everywhere.': 0.84; 'self.value': 0.84; 'hand,': 0.93 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:from:date:message-id:subject:to :content-type; bh=PzegyFAyIBH7MvQkwhNhm4bIakvCEczP/FPYz6Up0h8=; b=EtIX35bLdORvWvtrHodw7/oqkPy/TeGw19H42E+wun3SjykyplTNAMq/EYUwalt0uu oFh1WBiEx70VfntqTzQM1EMLg7hEmVHjVzBKkpOSE4SeD30apfW5+u7ztDjzzLBBl6d0 wmt7l7TTbOhVScZzZnGiW8+EnhLVilsRPYoag4YYFQkk9kIX6WzSFt/lo621ViewyNRn MXnBDb/y1+Tb5vhZsbh1rw5zMi2bHHKRuV1WGKjO378LjLEx3/NGf39Vlf6xvDd2CXXR MCrTsvoMyQ64lUCQ0TthxtjQw+D48rheiCROLtoNOOrqGMEVSPash+9YVkiEHusDKF4P bcvA== X-Received: by 10.68.191.39 with SMTP id gv7mr272742pbc.90.1395905596874; Thu, 27 Mar 2014 00:33:16 -0700 (PDT) MIME-Version: 1.0 In-Reply-To: <53318664$0$29994$c3e8da3$5496439d@news.astraweb.com> References: <9daf0806-02de-4447-964c-c8f8953c23e5@googlegroups.com> <532d5bd9$0$29994$c3e8da3$5496439d@news.astraweb.com> <0b78649a-16b3-4410-8258-e859578d62be@googlegroups.com> <53060a97-44fb-4e53-a7a7-d5eeed416f62@googlegroups.com> <53318664$0$29994$c3e8da3$5496439d@news.astraweb.com> From: Ian Kelly Date: Thu, 27 Mar 2014 01:32:36 -0600 Subject: Re: Time we switched to unicode? (was Explanation of this Python language feature?) To: Python Content-Type: text/plain; charset=ISO-8859-1 X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 90 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1395905606 news.xs4all.nl 2930 [2001:888:2000:d::a6]:49145 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:69174 On Tue, Mar 25, 2014 at 7:36 AM, Steven D'Aprano 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)