Case-insensitive string equality
Rick Johnson
rantingrickjohnson at gmail.com
Mon Sep 4 18:49:59 EDT 2017
Steven D'Aprano wrote:
[...]
> (1) Add a new string method, which performs a case-
> insensitive equality test. Here is a potential
> implementation, written in pure Python:
>
> def equal(self, other):
> if self is other:
> return True
> if not isinstance(other, str):
> raise TypeError
> if len(self) != len(other):
> return False
> casefold = str.casefold
> for a, b in zip(self, other):
> if casefold(a) != casefold(b):
> return False
> return True
>
> Alternatively: how about a === triple-equals operator to do
> the same thing?
A good idea. But wouldn't that specific usage be
inconsistent (even backwards) with the semantics of "===" as
defined in most languages that use "==="?
For me -- and this comment will be going beyond the scope of
strings, and possibly, beyond the scope of this thread -- i
feel that python is missing a pair of equality testing
devices (sugared or not; but preferably sugared), that
define a universal means by which all types can be tested
with either "superficial equality" (aka: ==) or "deep
equality" (aka: ===).
However, such a design (whist quite intuitive) would break
equality testing as it exists today in Python. For instance,
it would mean that:
(1) Superficial Equality
>>> "abc" == "abc"
True
>>> "abc" == "ABC"
True
(2) Deep Equality
>>> "abc" === "abc"
True
>>> "abc" === "ABC"
False
And i don't think even GvR's time machine will be much
help here. :-(
More information about the Python-list
mailing list