On Wed, Mar 30, 2016 at 1:08 AM, Antoon Pardon <antoon.par...@rece.vub.ac.be> 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 -- https://mail.python.org/mailman/listinfo/python-list