Path: csiph.com!fu-berlin.de!uni-berlin.de!not-for-mail From: Chris Angelico Newsgroups: comp.lang.python Subject: Re: Suggestion: make sequence and map interfaces more similar Date: Wed, 30 Mar 2016 11:56:33 +1100 Lines: 48 Message-ID: References: <56f8836b$0$1602$c3e8da3$5496439d@news.astraweb.com> <56FA8C71.4050306@rece.vub.ac.be> Mime-Version: 1.0 Content-Type: text/plain; charset=UTF-8 X-Trace: news.uni-berlin.de Miqm/lI2CIbHSfqGke6mGwVz1+4nxWptszZE+ku/qnrQ== Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.020 X-Spam-Evidence: '*H*': 0.96; '*S*': 0.00; 'cc:addr:python-list': 0.09; 'dict': 0.09; 'likewise': 0.09; 'violates': 0.09; 'example:': 0.10; 'itself.': 0.11; 'index': 0.13; 'def': 0.13; 'wed,': 0.15; '2016': 0.16; 'contract.': 0.16; 'from:addr:rosuav': 0.16; 'from:name:chris angelico': 0.16; 'mapping,': 0.16; 'received:io': 0.16; 'received:psf.io': 0.16; 'self[key]': 0.16; 'sequence,': 0.16; 'stability.': 0.16; 'subject:interfaces': 0.16; 'subject:make': 0.16; 'wrote:': 0.16; 'changes': 0.20; 'cc:2**0': 0.20; 'cc:addr:python.org': 0.20; 'arguments': 0.22; 'am,': 0.23; 'this:': 0.23; 'header:In-Reply-To:1': 0.24; 'chris': 0.26; 'earlier': 0.27; 'message-id:@mail.gmail.com': 0.27; 'sequence': 0.27; 'closer': 0.29; 'index,': 0.29; 'key,': 0.29; 'received:209.85.213.174': 0.29; 'that.': 0.30; 'class.': 0.30; 'push': 0.30; 'class': 0.33; 'items.': 0.33; 'steven': 0.33; 'definition': 0.34; 'add': 0.34; 'received:google.com': 0.35; 'mapping': 0.35; 'stable': 0.35; 'item': 0.35; 'but': 0.36; 'received:209.85': 0.36; 'subject:: ': 0.37; 'two': 0.37; 'expect': 0.37; 'received:209.85.213': 0.37; 'received:209': 0.38; 'stuff': 0.38; 'goes': 0.39; 'does': 0.39; 'still': 0.40; 'some': 0.40; 'your': 0.60; 'relationship': 0.61; "you'll": 0.61; 'subject:more': 0.61; 'further': 0.62; 'bring': 0.62; 'back': 0.62; '30,': 0.63; 'march': 0.64; 'other.': 0.64; 'mar': 0.65; 'elsewhere': 0.66; 'python-list': 0.66; 'safety': 0.66; 'chrisa': 0.84; 'lacks': 0.84; 'terrible': 0.84; 'to:none': 0.91; 'sulla': 0.91; 'hand,': 0.97 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:date:message-id:subject:from:cc; bh=h3IrETzb+l5e3Zf3gUBPjplrSFEuiDJyyILhO+8Alyg=; b=uQ+aVcdok4/7bTDUnIbASHtqWvav3H6iPqn+J8/bHYc+sRYMnSXpKwOezWNbhwZeqP jff87MxJ6Nyi3NPYlfSKSUxe5iDCmv+O6gp9Ut3txuqQDM1jY7j7Z4uc87Ic6a398KcG G6TbDT4Y0hdw24Ho3K54FZgtzDgV6uvv3AkUXy0nNBmtV26MDDEIecK4UHMAg4UaWicj Y4AhMr69Eg8uRKpuqsphHfztk+B9z15Y02m2W0npccP7VyJUNIfaPqSK6wyfmGK80c91 ELDPDvutbDsESRiL4zgX8OZEhXeVP3opBcjqaI/TWRooD2Z5eH2XPr0hoSL7i4YDb7n1 o/1Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:mime-version:in-reply-to:references:date :message-id:subject:from:cc; bh=h3IrETzb+l5e3Zf3gUBPjplrSFEuiDJyyILhO+8Alyg=; b=F/0shwQXRLi8a0fRkGJrWcWR3jbPvSDIhUOgzlTGs8BuqOewnEwr43fhSdHsdvAy4b QdqBV5QTULlRrBp3yjyVu8172Dq2A44uxkJB6bZlrIxa1L8E2pRm/nP20CLhPXixwbgr s2oTVxmgOhLujOd1z8VcCsR03ikRzo50sfmoW95qzFpbuWir0h6l5taKIXkDaEt9XXSF HUaGxpZwiQFtuYe8O1v296n0WuhME/VkfSjrfC8LxoGYZ2Om0vrLUus1sKwwgAbc1xd8 xjwMCCCAlEImAzbFYHTVQlFcf2komjFvkdPfPsBa3xr7BYmqoB8ORG0un71tTJPPOatx DN+Q== X-Gm-Message-State: AD7BkJIBlZ5oN2LNyVF4WYJfu+iXmPuh1eyFVBkyT8l/6qlEEydpndo+CY80tE48a5eWD+ma1DE4p9bV2yRiTA== X-Received: by 10.50.112.169 with SMTP id ir9mr19451690igb.92.1459299393773; Tue, 29 Mar 2016 17:56:33 -0700 (PDT) In-Reply-To: X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Xref: csiph.com comp.lang.python:106023 On Wed, Mar 30, 2016 at 9:29 AM, Marco Sulla via Python-list wrote: > On 29 March 2016 at 16:31, Chris Angelico wrote: >> But the definition of a sequence, and likewise the definition of a >> mapping, goes deeper than that. A sequence has *relative* stability; >> if one item is at a lower index than another, it will continue to be >> at a lower index, until you change one of those two items. Changes >> elsewhere in the sequence might bring them closer together or push >> them further apart, but one of them is still earlier in the sequence >> than the other. A mapping, on the other hand, has *absolute* >> stability. Any given key->value relationship is stable in and of >> itself. If you stuff a thing into a dict under a particular key, you >> expect to be able to get it back using that key, not some other. > > It's the same arguments of Steven D'Aprano. Let me counter these > arguments with this example: > >>>> class StrangeDict(dict): > ... def insert(self, k, v): > ... if k in self: > ... for key in [x for x in sorted(self.keys()) if x >= k]: > ... v_next = self[key] > ... self[key] = v > ... v = v_next > ... > ... self[key+1] = v > ... >>>> a = StrangeDict({0:5, 1:7, 2: 14}) >>>> a.insert(1, 5) >>>> a > {0: 5, 1: 5, 2: 7, 3: 14} > > Yes, it's a terrible class and lacks a lot of safety checks, but I > don't think at all it violates the map contract. You can continue and > add some constraint at __init__() about key types and their > contiguity, some other methods and voila', you'll get a list-like > class. The map contract is this: x = StrangeDict() x[123] = 456 ... assert x[123] == 456 Your mapping does violate the map contract. ChrisA