[Python-ideas] checking for identity before comparing built-in objects

Victor Stinner victor.stinner at gmail.com
Thu Oct 4 17:08:40 CEST 2012

2012/10/4 Steven D'Aprano <steve at pearwood.info>:
> On 04/10/12 21:48, Max Moroz wrote:
>> It seems that built-in classes do not short-circuit `__eq__` method
>> when the objects are identical, at least in CPython:
>>      f = frozenset(range(200000000))
>>      f1 = f
>>      f1 == f # this operation will take about 1 sec on my machine
> You shouldn't over-generalize. Some built-ins do short-circuit __eq__
> when the objects are identical. I believe that strings and ints both
> do. Other types might not.

This optimization is not implemented for Unicode strings.

PyObject_RichCompareBool() implements this optimization which leads to
incorrect results:

nan = float("nan")
mytuple = (nan,)
assert mytuple != mytuple # fails

I think that the optimization should be implemented for Unicode
strings, but disabled in PyObject_RichCompareBool().

@Max Moroz: Can you please open an issue on bugs.python.org?


More information about the Python-ideas mailing list