On 24Dec2022 14:35, Chris Angelico <ros...@gmail.com> wrote:
On Sat, 24 Dec 2022 at 13:15, Cameron Simpson <c...@cskk.id.au> wrote:
My question was more: do you know, or do you have to look? I'll take
another example. Take the list.index() method, which returns the index
where a thing can be found. *Without checking first*, answer these
questions:

1) Will identical-but-not-equal values (eg the same instance of nan) be found?

I'd say no, because it should need to do an equality check to compare things.

Let's see:

    >>> from math import nan
    >>> nan == nan
    False
    >>> L = [nan]
    >>> L.index(nan)
    0

Well, I'm wrong. Assuming it does a precheck with "is", I guess.

2) Do the docs and/or docstring tell you the answer to question 1?

[ To the docs!... How disappointing, the "Index" href at top right does not take me directly to list.index :-) ]

help(list.index) seems empty. The best docs seem to be .index for sequences: https://docs.python.org/3/library/stdtypes.html#index-19
which only allude to the intended semantics:

index of the first occurrence of x in s (at or after index i and before index j)

I guess that though _could_ imply an object identify check, but if I read it that way I might really take it to mean identity ("occurrence of x in s"), rather than the more useful and expected "also equal".

The "nan" test says to me that there's an identity check, which is at least quite fast and maybe significantly useful for types which intern small values like int.

This test says there's a fallback to an equality test:

    >>> L1=[3]
    >>> L2=[3]
    >>> L1 is L2
    False
    >>> L1 == L2
    True
    >>> L1 in [L2]
    True

And then a logical followup:

3) If your answer to question 1 was incorrect, {does it help, would it
have helped} to have a note in the docs?

It would help to be able to understand the behaviour. I think with `list.index` I'd expect an equality test only (I was surprised by your "nan" example, even though "nan" is a pretty unusual value).

Cheers,
Cameron Simpson <c...@cskk.id.au>
_______________________________________________
Python-ideas mailing list -- python-ideas@python.org
To unsubscribe send an email to python-ideas-le...@python.org
https://mail.python.org/mailman3/lists/python-ideas.python.org/
Message archived at 
https://mail.python.org/archives/list/python-ideas@python.org/message/FIL23Q4SBDWUWMH4NOLGVIMCI7AGQGMY/
Code of Conduct: http://python.org/psf/codeofconduct/

Reply via email to