Suggestion: make sequence and map interfaces more similar

Antoon Pardon antoon.pardon at rece.vub.ac.be
Wed Mar 30 15:07:29 EDT 2016


Op 30-03-16 om 17:56 schreef Steven D'Aprano:
> On Thu, 31 Mar 2016 12:12 am, Antoon Pardon wrote:
> 
>> Op 30-03-16 om 14:22 schreef Steven D'Aprano:
> 
> [...]
>>> Why is a mapping (such as a dict) best described as a surjection?
>>> Consider:
>>>
>>> d = {1: None, 2: 'a', 3: 'b', 4: None}
>>>
>>>
>>> Every key has exactly one value. There are no values without a key, and
>>> every value has *at least* one key.
>>
>> That second remark depends on what you consider the codomain. You could
>> of course define the codomain as the set of actual values in the mapping,
>> but that seems to be very artificial since it means that the codomain can
>> changes any time a value is changed, added or removed.
> 
> But that's exactly what the mapping does.

No it generally doesn't. I see no reason when at one moment I can write the following.

  assert map.values == {2, 5, 7, 9}
  map[3] = 8

to claim the codomain has changed. The actual function has changed yes but
since the application shows that the universe of possible mapping represented
by this specific variable containes functions in which 8 is an element of
the function image. This is best expressed by saying 8 is part of the codomain
whether or not 8 is part of the actual values at any specific moment.

Sure you sometimes work with a map in which the image is the domain, like
you did with your map of a limit square function but I consider those
as exceptions, not the general case.


> And likewise for the codomain. We can't say that the codomain is all
> positive integers, since the mapping is not defined for any values of x
> except the given keys.
> 
> Because dicts are mutable, we can do this:
> 
> d[2] = 7
> 
> 
> but that's precisely what we *can't* do with mathematical functions! We
> can't just declare that, from now on, x**2 will equal 7 if x is 2. So any
> modification of the dict must give us a new and different function. Before,
> we had the function:

So? The best way IMO to look at this is as a family of functions between
a domain and a codomain. Because that is the problem space you are looking
at. If within this problem space you have found a surjection, the normal
way to understand that is that you have found a function in which the
image is the total of the possible value set.

> # d equivalent to
> f(x): {1,2,3,4} --> {1,4,9,16} where 
>     f(1) = 1
>     f(2) = 4
>     f(3) = 9
>     f(4) = 16
> 
> 
> After executing the line d[2] = 7, we have the completely different
> function:

So? Since this change of a function is presumably a step in searching
a solution within some problem space, the domain and codomain of both
functions are the same. Even if the function is not total nor surjective
in this way.

> That's the nature of dicts as mappings: they don't actually come very close
> to the semantics of mathematical functions. Any talk of surjections,
> domains, codomains etc is going to be, at best, an analogy.

At any specific time a dictionary is a perfect representation of a
mathematical function.

> So all of this discussion is stretching the mathematical terms to their
> breaking point. Dicts aren't actually functions in the mathematical sense,
> and in the general case, they don't have well-defined domains and
> codomains.

But that part of the problem you represent by the dictionary often enough
has a well defined domain and codomain.

> Because fundamentally, it doesn't matter whether dicts are surjections or
> not. They're still many-to-one mappings, and those mappings between keys
> and values should not change due to the insertion or deletion of unrelated
> keys.

Repeating your opion without further arguments doesn't lend any support
to that opinion. If within the problem space you are working on, such a change
would make sense as a step to generate the next mapping from the previous
one, i don't see what the problem is if one could make use of this.

> If the OP wants to create his own "StrangeDict", which he himself described
> as "terrible", then he can do so. But to make all dicts and lists behave in
> this "terrible" and "strange" way is a terrible idea.

Calling something a terrible idea isn't a strong arguement. Other "terrible"
ideas have made it in the language.

> [...]
>> Only if you use the special python meaning of (dictionary) values in this
>> context. If you use the ordinary (mathematical) meaning, there are lots of
>> values that don't have a key, like every two characters string.
> 
> Well, we're actually discussing Python dictionaries. So, damn straight, of
> course I'm going to use the meaning of words as they apply to dicts.

How so? We were talking about functions and how dicts could represent them.
So when next we talk about a characteristic of such functions I don't see
why we should use the meaning as they apply to dicts, since we weren't talking
about the dicts.

-- 
Antoon.





More information about the Python-list mailing list