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


Groups > comp.lang.python > #52355

Re: Elegant compare

Path csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!feeder.erje.net!eu.feeder.erje.net!xlned.com!feeder5.xlned.com!newsfeed.xs4all.nl!newsfeed2.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail
Return-Path <rosuav@gmail.com>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.011
X-Spam-Evidence '*H*': 0.98; '*S*': 0.00; 'else:': 0.03; 'none,': 0.07; 'attributes': 0.09; 'def': 0.12; '""):': 0.16; '__lt__(self,': 0.16; 'accordingly,': 0.16; 'from:addr:rosuav': 0.16; 'from:name:chris angelico': 0.16; 'subject:Elegant': 0.16; 'subject:compare': 0.16; 'wrote:': 0.18; 'aug': 0.22; 'question': 0.24; 'header:In-Reply-To:1': 0.27; 'am,': 0.29; 'message- id:@mail.gmail.com': 0.30; '"")': 0.31; 'comparison': 0.31; 'long.': 0.31; 'class': 0.32; 'run': 0.32; 'skip:_ 10': 0.34; 'could': 0.34; 'but': 0.35; 'received:google.com': 0.35; 'should': 0.36; 'so,': 0.37; 'being': 0.38; 'problems': 0.38; 'jason': 0.38; 'to:addr:python-list': 0.38; 'to:addr:python.org': 0.39; 'simply': 0.61; 'first': 0.61; 'real': 0.63; '2013': 0.98
DKIM-Signature v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:date:message-id:subject:from:to :content-type; bh=mrjhRDi1BjiY2oAjnMXBBZeO2sSHeut4724JFzAAll4=; b=zNTNQKIcclgYPhpaGiUbwoqxwSIKOX9/vF1fX4y8Zb07LCHzl7jl6oNzHBOc7ofDeC 0dgMW/1nTNeQ2oXfSXxkvXnOK7SdpDLl39ZBn3iezOrRT0i5c6A67MNpTdsSOt7sL7GV KDFsrVxulAWvfhuV1xILW4hCpWHkGpQarbRn2WMyX1rYXFZpPnbVqw3TcwJDDSJAy8mm YzeGJb8rE02ZRQ4Xqo5yE3qP6bw+l3fKSQBnDDv9ngvkvo8kctDHR/V4K5jmqK/Twl2n eEpGq36keUN2QnHncoH3PvdIFNVLbmDsjX/QQQNtsUkziJbE23shf0pjh/bx3AtqVbhJ 8aQw==
MIME-Version 1.0
X-Received by 10.58.19.136 with SMTP id f8mr3641823vee.98.1376192744750; Sat, 10 Aug 2013 20:45:44 -0700 (PDT)
In-Reply-To <CANy1k1hmMXCP8UxrGOCVu8rSuqSfRw5c-NB1Zs9z4L9bsyfvFQ@mail.gmail.com>
References <CANy1k1hmMXCP8UxrGOCVu8rSuqSfRw5c-NB1Zs9z4L9bsyfvFQ@mail.gmail.com>
Date Sun, 11 Aug 2013 04:45:44 +0100
Subject Re: Elegant compare
From Chris Angelico <rosuav@gmail.com>
To python-list@python.org
Content-Type text/plain; charset=ISO-8859-1
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.15
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <http://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list/>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <http://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.462.1376192748.1251.python-list@python.org> (permalink)
Lines 27
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1376192748 news.xs4all.nl 15935 [2001:888:2000:d::a6]:58668
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:52355

Show key headers only | View raw


On Sun, Aug 11, 2013 at 4:41 AM, Jason Friedman <jsf80238@gmail.com> wrote:
> 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?

The first question is: What should the comparison do with a None
value? Should it be considered less than every string? If so, you
could simply use:

if (self.attr1 or "") < (other.attr1 or ""):

which will treat any falsy value as blank.

ChrisA

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


Thread

Re: Elegant compare Chris Angelico <rosuav@gmail.com> - 2013-08-11 04:45 +0100

csiph-web