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 01:31:32 +1100 Lines: 62 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 /2xUmqPI8sYGLBKdx0YchwXKLxnVRW/zA5V4MmdnNw7g== 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; 'received:209.85.223': 0.03; 'type,': 0.07; 'cc:addr:python-list': 0.09; '(none,': 0.09; 'all).': 0.09; 'behave': 0.09; 'dict': 0.09; 'immutable': 0.09; 'integers': 0.09; 'iterate': 0.09; 'lengths': 0.09; 'likewise': 0.09; 'mess': 0.09; 'tuple': 0.09; 'tuple.': 0.09; 'python': 0.10; 'itself.': 0.11; 'index': 0.13; 'wed,': 0.15; '1:08': 0.16; '2016': 0.16; 'calendar:': 0.16; 'cleanly': 0.16; 'different,': 0.16; 'fine;': 0.16; 'from:addr:rosuav': 0.16; 'from:name:chris angelico': 0.16; 'keys"': 0.16; 'mapping,': 0.16; 'oddly': 0.16; 'ordereddict': 0.16; 'pairs': 0.16; 'received:io': 0.16; 'received:psf.io': 0.16; 'sequence,': 0.16; 'sequence.': 0.16; 'stability.': 0.16; 'subject:interfaces': 0.16; 'subject:make': 0.16; 'zero,': 0.16; 'wrote:': 0.16; 'why.': 0.18; 'library': 0.20; 'changes': 0.20; 'cc:2**0': 0.20; 'cc:addr:python.org': 0.20; 'handles': 0.20; '31,': 0.22; "aren't": 0.22; 'function,': 0.22; 'keys': 0.22; 'am,': 0.23; 'somewhere': 0.24; 'header:In- Reply-To:1': 0.24; "i've": 0.25; 'earlier': 0.27; 'define': 0.27; 'switch': 0.27; 'message-id:@mail.gmail.com': 0.27; 'sequence': 0.27; 'function': 0.28; 'idea': 0.28; 'about.': 0.29; 'closer': 0.29; 'figured': 0.29; 'index,': 0.29; 'key,': 0.29; 'sure,': 0.29; 'array': 0.29; 'handled': 0.29; "i'm": 0.30; 'that.': 0.30; 'push': 0.30; 'point': 0.33; 'problem': 0.33; 'items.': 0.33; 'lets': 0.33; 'steven': 0.33; 'values.': 0.33; 'similar': 0.33; 'definition': 0.34; 'add': 0.34; 'received:google.com': 0.35; 'best,': 0.35; 'acceptable': 0.35; 'mapping': 0.35; 'stable': 0.35; 'item': 0.35; 'but': 0.36; 'instead': 0.36; 'there': 0.36; 'received:209.85': 0.36; '(and': 0.36; 'subject:: ': 0.37; 'two': 0.37; 'agree': 0.37; 'expect': 0.37; 'associated': 0.38; 'difference': 0.38; 'presence': 0.38; 'received:209': 0.38; 'names': 0.38; 'stuff': 0.38; 'anything': 0.38; 'represent': 0.38; 'goes': 0.39; 'data': 0.39; 'sure': 0.39; 'still': 0.40; 'some': 0.40; 'your': 0.60; 'share': 0.61; 'relationship': 0.61; 'subject:more': 0.61; 'further': 0.62; 'bring': 0.62; 'back': 0.62; 'making': 0.62; 'course': 0.62; 'matter': 0.63; '30,': 0.63; 'more': 0.63; 'times': 0.63; 'other.': 0.64; 'between': 0.65; 'mar': 0.65; 'elsewhere': 0.66; 'talking': 0.67; 'act': 0.67; 'treat': 0.72; '11:': 0.84; '12:': 0.84; 'chrisa': 0.84; "d'aprano:": 0.84; 'dense': 0.84; 'dict,': 0.84; 'dict.': 0.84; 'forced': 0.84; 'inherent': 0.84; 'lacks': 0.84; 'pardon': 0.84; 'schreef': 0.84; 'absolutely': 0.88; 'to:none': 0.91; 'am.': 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=R3ue4w18Gt7WcueSZ7jLVbpl2osunGJakr6jXb/3GLc=; b=aEhmtZHFvA1L6DzvQHP69wM3rVaPNTBiqu03nSo9XmGyb2BbzE+3N/eZTfPymo210i M9J7oofuBmok2lZGT32am7aYhePZnqUat2y5wzbyNwcwNF8LpbNaIroYtgiOCjiBhaNN BrcEdQ2tECLDCbxXcGckzkJBxnI3Hdshj+bX7Plv9atfSUewxPXiwGycI3aga1YMv2tF HcBoytEWPBNUg1Fdfmd6BVSC1CuvGB1S8BwCk7Muu57pxxguntezUMeI92X+7PjFFTLa cVOlZeNqM7uadQeCmmK1/v35Fd4ZRLQM9d+rrF96HWMYzDq+PT5pwfinUKcISRAlfdpl KJ5w== 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=R3ue4w18Gt7WcueSZ7jLVbpl2osunGJakr6jXb/3GLc=; b=NoSfVwAgIlzOpzNIFMZD8gSYFs1735k9ppSdS1g7a5yNvsNJnItFM621V1SQVzQZxI BnPLGcebsN5D4jDFlFYq1WsgrFkVUmMAxTa4zwB7t0xzL1Le7Dr6HX93cTkHSLy7lh4I +W8pbqHS+wX2b0o+0D5O0E/jaD8VoWooklW1ojxg/w2YN6mzZi+XOqJexyD7L7IK/Ci0 Ohi55sMiN9bp6m7w7U5182xNzD2UVWbKEi4fpRi2r+fw4wya3nMs8NGSGoPIt+w1Tf69 jn62crA/sGGQMHMNO1cvwHNrfjZQSLnUgjtH+i5Hr/urGKHA2YlN7jfERtth6Tm1t7pk AwPA== X-Gm-Message-State: AD7BkJI73MG2L5V7IVw3gkRXO4QAoSk5JP3z0nz6YEwdcmOBn7eFt0K09NBRhP3ZI2FollP5LTN30k9BPEa6Tw== X-Received: by 10.107.63.139 with SMTP id m133mr2918539ioa.157.1459261892877; Tue, 29 Mar 2016 07:31:32 -0700 (PDT) In-Reply-To: <56FA8C71.4050306@rece.vub.ac.be> 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:105988 On Wed, Mar 30, 2016 at 1:08 AM, Antoon Pardon wrote: > Op 28-03-16 om 03:05 schreef Steven D'Aprano: >> When you add a new key:value to a dict, the other key:value pairs don't >> change. That is the whole point of a mapping! Of course you can >> deliberately change the value by re-assignment: >> >> map[key] = new_value >> >> but that's not what I'm talking about. When you add a NEW key, the OTHER >> keys DON'T change. That is ABSOLUTELY CRITICAL to a mapping. Anything which >> lacks that property is not a mapping. > > I'm not sure I agree with that. I am. The similarity between "mapping with integers as keys" and "sequence" is that both of them can be subscripted. And that's fine; Python lets you define __getitem__ in any way you like. OrderedDict is somewhere between "sequence with associated keys" and "mapping with inherent order", and there's no problem with that. 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. Sure, you can use a tuple to represent an immutable mapping between dense integers and values. And there are times when that's perfectly acceptable - here's two ways of depicting the month lengths in a non-leap year in the Gregorian calendar: mapping = {1: 31, 2: 28, 3: 31, 4: 30, 5: 31, 6: 30, 7: 31, 8: 31, 9: 30, 10: 31, 11: 30, 12: 31} sequence = (None, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31) Both of them let you look up 6 and get back that June has 30 days. The sequence is forced to start from zero, but that's a small matter (and if you number your months 0-11 instead of 1-12, no difference at all). But the only operation these two data types truly share is subscripting. If you want to be able to iterate over these in calendar order, you don't switch out the dict for an OrderedDict - you use the tuple. If you want to be able to use month names instead of numbers, you don't mess around with the tuple - you use the dict. 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, and standard library functions can behave oddly in the presence of all-numeric keys that aren't intended to be a sequence. The two types are fundamentally different, and you almost never want to treat them identically - at best, you want to have one specific function that handles either type, and that's most cleanly handled inside that function, not by adding mapping-like features to sequences or vice versa. ChrisA