New submission from Rupert Tombs :
operator.countOf behaves differently in its docstring and its c and python
implementations when `is` and `==` differ.
help(countOf) ->
countOf(a, b, /)
Return the number of times b occurs in a.
This could be read to say that it returns equal to `sum(x is b for x in a)`.
Its python implementation returns `sum(x == b for x in a)`.
Since its c implementation uses `PyObject_RichCompareBool`, it returns `sum(x
is b or x == b for x in a)`.
Results of these implementations can differ when `x is b` does not imply `x ==
b`;
that could be from an __eq__ method, but the the float NaN is a real example
since NaN != NaN.
The issue is demonstrated with a possible fix here
https://godbolt.org/z/cPT7TToG7
Since the c version has been in the wild for decades, I suggest that it should
be taken to define the function;
the python should be updated to match it, and the docstring could say
"Return the number of items in a which are, or which equal, b."
I will open a pull request with these changes shortly.
--
assignee: docs@python
components: Documentation, Library (Lib)
messages: 396917
nosy: docs@python, rtombs
priority: normal
severity: normal
status: open
title: operator.countOf `is` / `==` inconsistency
type: behavior
versions: Python 3.10, Python 3.11, Python 3.6, Python 3.7, Python 3.8, Python
3.9
___
Python tracker
<https://bugs.python.org/issue44558>
___
___
Python-bugs-list mailing list
Unsubscribe:
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com