Path: csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!news.albasani.net!feeder.news-service.com!news2.euro.net!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.000 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'subject:" ': 0.03; 'subject:test': 0.05; 'backwards': 0.07; 'seemed': 0.07; 'python': 0.07; 'dict': 0.09; 'false,': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:80.91.229.12': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'received:lo.gmane.org': 0.09; 'referenced': 0.09; 'that).': 0.09; 'types:': 0.09; '>>>': 0.12; 'def': 0.13; ';-)': 0.14; 'wrote:': 0.14; 'equality.': 0.16; 'false)': 0.16; 'keyerror:': 0.16; 'piraeus': 0.16; 'received:dip.t-dialin.net': 0.16; 'received:t-dialin.net': 0.16; 'remembered': 0.16; 'subject:was': 0.16; 'traceback': 0.16; '(most': 0.16; 'discussion': 0.20; 'performing': 0.22; 'subject:]': 0.22; 'convert': 0.22; '(and': 0.22; 'last):': 0.23; 'wonder': 0.24; 'worked': 0.24; 'preferred': 0.26; 'testing': 0.28; 'beyond': 0.28; 'raise': 0.29; 'error': 0.29; 'minimal': 0.29; 'comparison': 0.31; 'from:addr:web.de': 0.31; 'subject: [': 0.31; 'turns': 0.31; 'typeerror:': 0.31; 'to:addr:python-list': 0.32; '...': 0.32; 'url:docs': 0.33; 'uses': 0.34; 'header:X-Complaints-To:1': 0.34; 'actually': 0.34; 'file': 0.35; 'expensive': 0.35; '"",': 0.35; 'test.': 0.35; 'think': 0.36; 'url:python': 0.37; 'but': 0.38; 'url:org': 0.38; 'received:org': 0.38; 'set': 0.39; 'to:addr:python.org': 0.39; 'header:Mime-Version:1': 0.39; 'sets': 0.40; 'would': 0.40; "it's": 0.40; 'header:Received:5': 0.40; '2011': 0.62; 'making': 0.62; 'confirm': 0.71; 'link?': 0.84; 'moot': 0.84 X-Injected-Via-Gmane: http://gmane.org/ To: python-list@python.org From: Peter Otten <__peter__@web.de> Subject: Re: dict comparison [was: suggestions, comments on an "is_subdict" test] Date: Fri, 22 Apr 2011 21:28:04 +0200 Organization: None References: Mime-Version: 1.0 Content-Type: text/plain; charset="ISO-8859-1" Content-Transfer-Encoding: 7Bit X-Gmane-NNTP-Posting-Host: p5084c898.dip.t-dialin.net X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.12 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: 52 NNTP-Posting-Host: 82.94.164.166 X-Trace: 1303500482 news.xs4all.nl 81483 [::ffff:82.94.164.166]:35163 X-Complaints-To: abuse@xs4all.nl Xref: x330-a1.tempe.blueboxinc.net comp.lang.python:3887 Zero Piraeus wrote: > : > > On 22 April 2011 13:30, Peter Otten <__peter__@web.de> wrote: >>>>> def is_subdict(test_dct, base_dct): >> ... return test_dct <= base_dct and all(test_dct[k] == base_dct[k] >> for ... k in test_dct) >> ... >>>>> is_subdict({1:0}, {2:0}) >> Traceback (most recent call last): >> File "", line 1, in >> File "", line 3, in is_subdict >> File "", line 3, in >> KeyError: 1 >> >> I think you have to convert to sets before performing the <= comparison >> to get a proper subset test. > > Huh. I thought I remembered that dict comparison worked like set > comparison (and my admittedly minimal testing seemed to confirm that). > Turns out it's actually "consistent, but not otherwise defined" beyond > equality. > > http://docs.python.org/reference/expressions.html#id15 > > I maintain that the behaviour I expected makes more sense ;-) > I wonder > whether making it work the way I want it to (dammit) would have been > as prohibitively expensive as the lexicographical comparison mentioned > in the footnote referenced in the above link? The discussion is moot for Python 2, and because of backwards compatibility has been for a long time. Python 3 uses a different approach: >>> {1:0} < {1:0} Traceback (most recent call last): File "", line 1, in TypeError: unorderable types: dict() < dict() This is the behaviour that makes the most sense to me. I would have preferred attempts at set comparison to raise the same error which would have prevented surprises like >>> a, b = {1}, {2} >>> a < b, a > b, a == b (False, False, False) >>> a, b = sorted([{1}, {2}]) >>> a <= b False