Python less error-prone than Java

Peter Otten __peter__ at web.de
Sun Jun 4 04:00:43 EDT 2006


Kaz Kylheku wrote:

> Would that be a case-insensitive lexicographic comparison, or
> case-insensitive? How do you specify what kind of less-than and equal
> you want to do?

class Key(object):
    def __init__(self, value, key):
        self.keyval = key(value)
        self.key = key
    def __lt__(self, other):
        return self.keyval < self.key(other)
    def __gt__(self, other):
        return self.keyval > self.key(other)

items = ["Alpha", "Beta", "Delta", "Gamma"]
print binarySearch(items, Key("DELTA", str.lower)) # 2

You /can/ teach an old duck new tricks :-)

Peter
 




More information about the Python-list mailing list