Boilerplate in rich comparison methods
Paul Rubin
http
Sat Jan 13 02:28:06 EST 2007
More information about the Python-list mailing list
Sat Jan 13 02:28:06 EST 2007
- Previous message (by thread): Boilerplate in rich comparison methods
- Next message (by thread): Boilerplate in rich comparison methods
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Steven D'Aprano <steve at REMOVE.THIS.cybersource.com.au> writes: > class Parrot: > def __eq__(self, other): > return self.plumage() == other.plumage() > def __ne__(self, other): > return self.plumage() != other.plumage() > def __lt__(self, other): > return self.plumage() < other.plumage() > def __gt__(self, other): > return self.plumage() > other.plumage() > def __le__(self, other): > return self.plumage() <= other.plumage() > def __ge__(self, other): > return self.plumage() >= other.plumage() If it's that uniform I think you can just use __cmp__: class Parrot: def __cmp__(self, other): return cmp(self.plumage(), other.plumage()) Did I miss something? The idea of rich comparison is that the different relations aren't so similar to each other, e.g. some kind of partial ordering. > If the comparison requires a lot of work, I'll do something like this: > > class Aardvark: > def __le__(self, other): > return lots_of_work(self, other) > def __gt__(self, other): > return not self <= other > # etc. > > But I can't help feeling that there is a better way. What do others do? Same as above.
- Previous message (by thread): Boilerplate in rich comparison methods
- Next message (by thread): Boilerplate in rich comparison methods
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
More information about the Python-list mailing list