Path: csiph.com!fu-berlin.de!uni-berlin.de!not-for-mail From: Marco Sulla Newsgroups: comp.lang.python Subject: Re: Suggestion: make sequence and map interfaces more similar Date: Wed, 30 Mar 2016 00:29:26 +0200 Lines: 64 Sender: forwardedby@e4ward.com Message-ID: References: <56f8836b$0$1602$c3e8da3$5496439d@news.astraweb.com> <56FA8C71.4050306@rece.vub.ac.be> Reply-To: mail.python.org@marco.sulla.e4ward.com Mime-Version: 1.0 Content-Type: text/plain; charset=UTF-8 X-Trace: news.uni-berlin.de o1nWnQRFrl77ART7cgdO6wZ7AbsY+8x2lWr7ZNXHAerA== Return-Path: <31489.gmail.com@bounce.e4ward.com> X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.003 X-Spam-Evidence: '*H*': 0.99; '*S*': 0.00; 'received:209.85.223': 0.03; 'pop': 0.05; 'method,': 0.07; 'shortcut': 0.07; 'dict': 0.09; 'immutable': 0.09; 'likewise': 0.09; 'mutable': 0.09; 'violates': 0.09; 'example:': 0.10; 'itself.': 0.11; 'index': 0.13; 'def': 0.13; '2016': 0.16; 'contract.': 0.16; 'mapping,': 0.16; 'received:io': 0.16; 'received:psf.io': 0.16; 'self[key]': 0.16; 'sequence,': 0.16; 'slice.': 0.16; 'stability.': 0.16; 'subject:interfaces': 0.16; 'subject:make': 0.16; 'wrote:': 0.16; 'why.': 0.18; '>>>': 0.20; 'changes': 0.20; 'arguments': 0.22; 'lawrence': 0.22; 'parameter': 0.22; 'tuples': 0.22; 'trying': 0.22; 'code,': 0.23; 'header:In-Reply-To:1': 0.24; "i've": 0.25; 'chris': 0.26; 'earlier': 0.27; 'point.': 0.27; 'checking': 0.27; 'message-id:@mail.gmail.com': 0.27; 'sequence': 0.27; 'idea': 0.28; 'interface': 0.29; 'closer': 0.29; 'figured': 0.29; 'index,': 0.29; 'key,': 0.29; 'separated': 0.29; 'array': 0.29; 'allows': 0.30; 'that.': 0.30; 'class.': 0.30; 'push': 0.30; 'useful': 0.33; 'class': 0.33; 'problem': 0.33; 'common': 0.33; 'items.': 0.33; 'programming,': 0.33; 'steven': 0.33; 'similar': 0.33; 'definition': 0.34; 'add': 0.34; 'received:google.com': 0.35; 'header:Received:8': 0.35; 'mapping': 0.35; 'stable': 0.35; 'item': 0.35; 'but': 0.36; 'should': 0.36; 'received:209.85': 0.36; 'to:addr:python-list': 0.36; 'subject:: ': 0.37; 'two': 0.37; 'expect': 0.37; 'method': 0.37; 'received:209': 0.38; 'stuff': 0.38; 'why': 0.39; 'goes': 0.39; 'sure': 0.39; 'easily': 0.39; 'to:addr:python.org': 0.40; 'mark': 0.40; 'still': 0.40; 'some': 0.40; 'relationship': 0.61; "you'll": 0.61; 'subject:more': 0.61; 'further': 0.62; 'bring': 0.62; 'back': 0.62; 'making': 0.62; 'more': 0.63; 'march': 0.64; 'other.': 0.64; 'elsewhere': 0.66; 'safety': 0.66; 'header:Reply-To:1': 0.67; 'act': 0.67; 'reply-to:no real name:2**0': 0.71; 'distinguish': 0.84; 'lacks': 0.84; 'received:23': 0.84; 'terrible': 0.84; 'hand,': 0.97 Authentication-Results: dfw.e4ward.com (amavisd-new); dkim=pass (1024-bit key) reason="pass (just generated, assumed good)" header.d=e4ward.com DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=e4ward.com; h= reply-to:content-type:content-type:subject:subject:message-id :date:date:from:from:references:in-reply-to:mime-version; s= mail; t=1459290614; x=1459377015; bh=B2tP86RcF7P7WOR+JG14DlJzUOU xlSHVQfVN60BPyTM=; b=Jat050IG8eUCv1NRDFparFOACzyq56Lap0nh6hs0ITC A6mfGirWhCh/+aG25WpgGKVDRcq8E6Z241aKlsNgKIqI4DByW0p4Znhm4tA+0BOz 41C1DwfVzYilKbc6JLWhZgi731fbatiqumZ6jYkmxbJy/jY82Vq9isAw2VefQJ7o = Authentication-Results: dfw.e4ward.com (amavisd-new); dkim=fail (2048-bit key) reason="fail (message has been altered)" header.d=gmail.com DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:sender:in-reply-to:references:from:date:message-id :subject:to; bh=B2tP86RcF7P7WOR+JG14DlJzUOUxlSHVQfVN60BPyTM=; b=zF6DzvCBTrkeVKAU9Fg61y5ltf0HBYS3STXPgj4qdCzrRh9T9kr0qmLa8ZP+zb3nuK WZj+CWZZDVyWO7ilEeuQ1hW3Y+B6BANWdKuwB/EA63EL+3c3KSOW5e1bxPVtz+In3OFh 83Ni15N3jGeQBIfoG/68d6MotUZFXbG9xJsRA7TLHCPE75njbfVRkntXBjaUJpef/4+4 zQW4xtquyoL68FmNMNCllGMRIr10g5+HrhoGU/eMKwOVj4FbfYWcfPU1IA0otqQuqJnG 0CqjkvaGlEia74xHnG0iHBwcP/0dsaH4E+WhvCskyr42TM6VOHpPLL8IG0yiORjPk1PW wyuw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:mime-version:sender:in-reply-to:references:from :date:message-id:subject:to; bh=B2tP86RcF7P7WOR+JG14DlJzUOUxlSHVQfVN60BPyTM=; b=CrIx3ZY8RERZSHSfWWT3XQFZTkHJGTsRR5hYzgoClEdABgLhKeR77gBBHWPcN5fV3D jIvVTpQgT7VGiW+Ah9K8xix21o6ylRF45GtA+slpwVDPj82i14DKoIPtdvjq1fJ2+EKr Ua3oMaX+lYaZWGhxNO95WJkC+ke6dBkhdRbizxxIFjQM6xhEJfzTQDWq7kMKzQmMfvMj 9ruvl+gidPwfN4vJQihsIAOn9H7FU69eDAAnRNsjNflo27xxldnvT+c3Po+qJymbAYnJ jIKqetwPY9enY9nITNiO25hR0PdUwBIkGbJ0gzHxZWbzvPi0/r7a/+PiymxJwA/Y0CkT E+KQ== X-Gm-Message-State: AD7BkJKnyorG/bemO5hlL0IQWDEK2R8P+i11IzeCdMYhrE36+7G5UIYjpRNfpHIPnJ2iZgqCPS+/nEHdimHWtw== X-Received: by 10.107.132.149 with SMTP id o21mr5211915ioi.118.1459290606407; Tue, 29 Mar 2016 15:30:06 -0700 (PDT) In-Reply-To: X-Google-Sender-Auth: w5St9Ueu2iXbLBT9mJ40x_0tZO0 X-e4ward-RCPT: python-list.python.org-mail.python.org.marco.sulla.e4ward.com@ftcw4rx8877.reply.e4ward.com X-e4ward-x: . 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:106012 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. Let me add that an items() and keys() for sequences will be also useful for day-by-day programming, since they will be a shortcut for enumerate(seq) and range(len(seq)) > The whole idea of making sequences and mappings more similar is highly > distasteful to me, and I've figured out why. It's the exact problem > with the PHP array - it's not sure whether to act as a mapping or a > sequence This is a good point. This is not true for mutable sequences (lists), since they will have a lot of methods (append, pop etc) that allows you to distinguish them, but immutable sequences like tuples will be not easily distinguishable using duck typing. I think the main problem is in __getitem__. __getitem__ can be used as slice method checking if its parameter is a slice. If the slice method was a separated method, this problem will not arise. On 27 March 2016 at 21:24, Mark Lawrence wrote: > Why do you need a new interace if all you're trying to do is create a > vdict class that has "iter(d) == iter(d.values()), and should also > have a count() method, like sequence types"? Since simply adding get(), items(), keys(), values() to existing sequence interface will likely break existing code, I would try also to write new sequence types, using a common interface with maps. This is my idea.