Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #52393

Re: Elegant compare

References <mailman.460.1376192467.1251.python-list@python.org> <52074ab3$0$30000$c3e8da3$5496439d@news.astraweb.com>
Date 2013-08-11 09:17 -0600
Subject Re: Elegant compare
From Jason Friedman <jsf80238@gmail.com>
Newsgroups comp.lang.python
Message-ID <mailman.486.1376236134.1251.python-list@python.org> (permalink)

Show all headers | View raw


> This is a hard question to answer, because your code snippet isn't
> clearly extensible to the case where you have ten attributes. What's the
> rule for combining them? If instance A has five attributes less than
> those of instance B, and five attributes greater than those of instance
> B, which wins?

Yes, my code snippet was too short, I should have said:

class my_class:
    def __init__(self, attr1, attr2, attr3):
        self.attr1 = attr1 #string
        self.attr2 = attr2 #string
        self.attr3 = attr3 #string
    def __lt__(self, other):
        if self.attr1 < other.attr1:
            return True
        elif self.attr2 < other.attr2:
            return True
        else:
            return self.attr3 < other.attr3

Chris's answer is actually perfectly adequate for my needs.
Thank you Steve and Chris.

Back to comp.lang.python | Previous | NextPrevious in thread | Find similar | Unroll thread


Thread

Elegant compare Jason Friedman <jsf80238@gmail.com> - 2013-08-10 21:41 -0600
  Re: Elegant compare Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2013-08-11 08:26 +0000
    Re: Elegant compare Jason Friedman <jsf80238@gmail.com> - 2013-08-11 09:17 -0600

csiph-web