Suggestion: make sequence and map interfaces more similar

Jussi Piitulainen jussi.piitulainen at helsinki.fi
Wed Mar 30 10:25:38 EDT 2016


Steven D'Aprano writes:

> On Wed, 30 Mar 2016 10:21 pm, Jussi Piitulainen wrote:
>
>> Ok, safer to say that some many-to-one mappings are not surjective.
>
> Can you give an example of a Python dict which is not surjective?

Can you give an example of a mapping which is not surjective? Can you
represent it as a Python dict?

Surjectivity requires a specified codomain. Python dicts as such do not
have one.

The Wikipedia article that you cite (the one that doesn't say what you
think it says) is about functions f that have a specified domain, which
is some set A, and a specified codomain, which is some set B. They
indicate this by writing f : A -> B. (I'm not sure if I need to point
out that the A and B in that notation in the text need not have anything
to do with the A and B in the diagrams next to the text on top of the
page. Instead they correspond to the X and Y in the diagrams.)

Do you think {1: 'D', 2: 'B', 3: 'A'} is surjective?

Do you think {1: 'd', 2: 'd', 3: 'c'} is surjective?

Those are the Wikipedia examples of non-surjective mappings on top of
the page.

Strictly speaking, these dicts represent the *graphs* of the functions,
and set("ABD") and set("cd") are the *images* of the two mappings. The
specified codomains are not recoverable from the dicts. (Do I need to
point out that the A, B, C, D and a, b, c, d in the diagrams are
abstract placeholders rather than characters or one-character strings
but using Python strings in their place does not change the abstract
structure of the data, and it's intended in these diagrams that they are
four different things when they are inside the same oval even though
that doesn't seem to be made explicit in the page?)

Yes, the restriction of any mapping to its image is surjective. No, this
does not make it even minimally informative to say of any particular
mapping that its restriction to its image is surjective - its image is
its image? Of course it is.

But it may be informative to say of a dict whether it has at least one
key for every value *in some intended codomain* - whether its image is
its codomain. That is surjectivity.

(I'm running out of pedantry, but just in case: I'm quite aware that I
didn't *explicitly* rule out the possibility of values in the dict but
not in the intended codomain. Should I have? It's part of the notion of
an intended codomain that values outside of it are not allowed at all.)

I don't think this has anything to do with the topic of the thread.



More information about the Python-list mailing list