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


Groups > comp.lang.python > #52353

Elegant compare

Date 2013-08-10 21:41 -0600
Subject Elegant compare
From Jason Friedman <jsf80238@gmail.com>
Newsgroups comp.lang.python
Message-ID <mailman.460.1376192467.1251.python-list@python.org> (permalink)

Show all headers | View raw


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

I will run into problems if attr1 or attr2 is None, and they
legitimately can be.

I know I can check for attr1 or attr2 or both being None and react
accordingly, but my real class has ten attributes and that approach
will be long.  What are my alternatives?

Back to comp.lang.python | Previous | NextNext 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