Is this safe? (Making string comparisons ignore case)

Martin von Loewis loewis at informatik.hu-berlin.de
Tue Jun 27 04:57:22 EDT 2000


Matthew Cline <matt at nightrealms.com> writes:

> I've done the following to make it so that all string comparisons
> are case insenstive.  Is this safe?  (I'm pleasently surprised that
> it actually works)

> def myCmp(self, other):
> 	return oldCmp(string.lower(self), string.lower(other))

That's quite incorrect, just try cmp(1,1). In general, replacing cmp
may have strange effects. Objects implementing __hash__ and __cmp__
typically implement them by delegating to hash() and cmp(). Such
objects would break when used as dictionary keys.

It is much better to use myCmp directly in the places where you need
it (and give it a different name, too!).

Regards,
Martin



More information about the Python-list mailing list