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

Steven D'Aprano steve at pearwood.info
Thu Oct 4 17:53:36 CEST 2012


On 05/10/12 01:08, Victor Stinner wrote:
> 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.

That does not match my experience. In Python 3.2, I generate a large
unicode string, and an equal but not identical copy:

s = "aЖcdef"*100000
t = "a" + s[1:]
assert s is not t and s == t


Using timeit, s == s is about 10000 times faster than s == t.



-- 
Steven



More information about the Python-ideas mailing list