Unexpected behaviour from the 'in' operator
Terry Reedy
tjreedy at udel.edu
Mon Feb 24 09:38:15 EST 2003
> [testing for identity instead of equality in seq]
for x in seq:
if x is y:
match = 1
break
else:
match = 0
# or
len(filter(lambda x: x is y, seq))
# does not short-circuit, counts multiple occurences
> If this is true I guess the equality operator could be optimized
> to first check for identity, and only test non-identical objects
> for equality.
For an intermediate check, compute and store a hash value with every
instance. Test hash equlity first and only then detail equality. For
instance, current stringobject.c/string_richcompare() uses the
equivalent of this:
def str_eq(a,b):
return len(a)==len(b) and a[0]==b[0] and a==b
It uses length and first char as quick hashes that eliminate most
non-matches.
Terry J. Reedy
More information about the Python-list
mailing list