Path: csiph.com!fu-berlin.de!uni-berlin.de!not-for-mail From: Antoon Pardon Newsgroups: comp.lang.python Subject: Re: Suggestion: make sequence and map interfaces more similar Date: Tue, 29 Mar 2016 16:08:49 +0200 Lines: 67 Message-ID: References: <56f8836b$0$1602$c3e8da3$5496439d@news.astraweb.com> Mime-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 7bit X-Trace: news.uni-berlin.de rJMR6WaBJTjc/9C3JOo7bw/xohSgMiUN14zfP5Y5VWnw== 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:134': 0.05; 'tries': 0.05; 'key.': 0.07; 'immutable': 0.09; 'mutable': 0.09; 'stable.': 0.09; 'violates': 0.09; 'index': 0.13; 'def': 0.13; 'missed': 0.15; 'argument': 0.15; '2016': 0.16; 'inserting': 0.16; 'intended.': 0.16; 'iterator': 0.16; 'mappings.': 0.16; 'opposite': 0.16; 'pairs': 0.16; 'received:ac.be': 0.16; 'received:io': 0.16; 'received:psf.io': 0.16; 'sequence.': 0.16; 'subject:interfaces': 0.16; 'subject:make': 0.16; 'wrote:': 0.16; 'result,': 0.18; 'try:': 0.18; 'language': 0.19; '>>>': 0.20; 'keys': 0.22; 'occurs': 0.22; 'produces': 0.22; 'suppose': 0.22; 'am,': 0.23; 'insert': 0.23; 'implemented': 0.24; 'header:In- Reply-To:1': 0.24; 'mon,': 0.24; 'sort': 0.25; 'header:User- Agent:1': 0.26; 'least': 0.27; 'sequence': 0.27; 'function': 0.28; 'values': 0.28; 'fine': 0.28; 'about.': 0.29; 'behaviour': 0.29; 'key,': 0.29; "i'm": 0.30; 'that.': 0.30; 'too.': 0.30; 'received:be': 0.30; 'implement': 0.32; 'generally': 0.32; 'returned': 0.32; 'point': 0.33; 'problem': 0.33; "d'aprano": 0.33; 'ones,': 0.33; 'steven': 0.33; 'values.': 0.33; 'except': 0.34; 'add': 0.34; 'lists': 0.34; 'list': 0.34; 'mapping': 0.35; 'maps': 0.35; 'something': 0.35; 'sometimes': 0.35; 'but': 0.36; 'too': 0.36; 'to:addr:python-list': 0.36; 'subject:: ': 0.37; 'agree': 0.37; 'method': 0.37; 'doing': 0.38; 'no,': 0.38; 'delete': 0.38; 'anything': 0.38; 'mean': 0.38; 'sure': 0.39; 'does': 0.39; 'takes': 0.39; 'to:addr:python.org': 0.40; 'some': 0.40; 'care': 0.60; 'relationship': 0.61; 'subject:more': 0.61; 'course': 0.62; 'more': 0.63; 'times': 0.63; 'between': 0.65; 'mar': 0.65; 'reverse': 0.66; 'talking': 0.67; "d'aprano:": 0.84; 'dict,': 0.84; 'lacks': 0.84; 'room.': 0.84; 'schreef': 0.84; 'absolutely': 0.88 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:38.0) Gecko/20100101 Icedove/38.6.0 In-Reply-To: <56f8836b$0$1602$c3e8da3$5496439d@news.astraweb.com> 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:105987 Op 28-03-16 om 03:05 schreef Steven D'Aprano: > On Mon, 28 Mar 2016 05:01 am, Marco S. wrote: > >> Steven D'Aprano wrote: >> >>> The point you might have missed is that treating lists as if they were >>> mappings violates at least one critical property of mappings: that the >>> relationship between keys and values are stable. >> >> This is true for immutable maps, but for mutable ones, you can simply do > No, it is true for mutable maps too. > > 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. > The relationship between the index of a value and the value in a sequence is > not stable. Inserting a new value can change the "key"(actually index) of > some or all of the existing values. Which is not simply adding a key. Inserting a new value is IMO more like doing an update. > The whole point of sequences is that > the position of values is NOT stable: they are intended to move around. I find that language too strong. The lists I use are generally stable. Does that mean I'm using lists in a way not intended. > You > can sort them, reverse them, delete them, insert new values, and the others > will move around to make room. If you think of the index as a key, this is > completely the opposite behaviour of mappings. Not really. IMO a mapping is just a surjective function and sometimes a list is a perfectly fine way to implement such a function. The point I think the OP tries to make is that sometimes you need to write a function that takes a surjective function as argument and produces some result, but you just don't care whether this function is implemented as a map or as a sequence. Suppose you want to know how many times each value occurs and want to map that. As it is you have to write something like the following. def count(l): counted = defaultdict(int) try: itr = l.values() except AttributeError: itr = iter(l) for v in itr: counted[v] += 1 return counted What would be the big problem if a values method would be available on a list which returned the iterator and if an items method would be like calling enumerate?