Path: csiph.com!feeder.erje.net!1.eu.feeder.erje.net!newsfeed0.kamp.net!newsfeed.kamp.net!fu-berlin.de!uni-berlin.de!not-for-mail From: Ian Kelly Newsgroups: comp.lang.python Subject: Re: how to convert code that uses cmp to python3 Date: Fri, 8 Apr 2016 08:17:17 -0600 Lines: 35 Message-ID: References: <57064D0D.1030701@rece.vub.ac.be> <87r3egfqiv.fsf@elektro.pacujo.net> <570778ad$0$1587$c3e8da3$5496439d@news.astraweb.com> Mime-Version: 1.0 Content-Type: text/plain; charset=UTF-8 X-Trace: news.uni-berlin.de nxhK8MHntvwAwuUfZ/D6BgAXkGORbg063m1pgH2yZFTA== Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.002 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'received:209.85.223': 0.03; 'method,': 0.07; 'subject:code': 0.07; 'cc:addr:python- list': 0.09; '[1,': 0.09; 'def': 0.13; '2016': 0.16; '__lt__(self,': 0.16; 'cc:name:python': 0.16; 'cmp': 0.16; 'comparisons,': 0.16; 'descending': 0.16; 'received:io': 0.16; 'received:psf.io': 0.16; 'sorted()': 0.16; 'subject:python3': 0.16; 'wrote:': 0.16; 'cheap': 0.18; 'integer': 0.18; 'tree': 0.18; '>>>': 0.20; 'cc:2**0': 0.20; 'cc:addr:python.org': 0.20; 'keys': 0.22; 'am,': 0.23; 'header:In-Reply-To:1': 0.24; 'compare': 0.27; 'fri,': 0.27; 'message-id:@mail.gmail.com': 0.27; 'fine': 0.28; 'comparison': 0.29; 'subject:that': 0.29; 'operations': 0.31; 'probably': 0.31; 'skip:_ 10': 0.32; 'expensive': 0.32; 'class': 0.33; "d'aprano": 0.33; 'steven': 0.33; 'received:google.com': 0.35; 'but': 0.36; 'should': 0.36; 'there': 0.36; 'received:209.85': 0.36; 'pm,': 0.36; 'subject:: ': 0.37; 'two': 0.37; 'received:209': 0.38; 'your': 0.60; 'believe': 0.66; 'insert,': 0.84; 'other.n': 0.84; 'pardon': 0.84; 'to:none': 0.91; 'self.n': 0.91 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:from:date:message-id:subject:cc; bh=A3KV4uaJSePVqRefotv+1xJ4pwpkLumOIBNqRVewKnk=; b=NjOt1lvCdJkaQMNqJ6PARUnwFDzlqA6MI2AMKk7HaTz7VixpbuU4+y86RqD0B6ko/T fAVUu6k3gnD0FzEk1UIJwPZY572HUh63zXS8c2jKLairNrCQ2dSS0BpFtyx6B8s8k24Y wCBTE1SglFCZpAr8ld+4znLfRZpQN4OimFUfKauiC45RVwU09r4aSaHFRl79AEYV6eNh JcHTF0bZ5I38TJttnKoZAw7xUIIa5xsTq7idQS5KlH4XbpypT/xEfAXD0RGidqY99CDm /rbG5sKdCq3qFWlzgxDBauXiYhvQckfKr6nGwPQgLYFk72UKkhyDOmRyK6lRnuU/8SrG RA5w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:mime-version:in-reply-to:references:from:date :message-id:subject:cc; bh=A3KV4uaJSePVqRefotv+1xJ4pwpkLumOIBNqRVewKnk=; b=TvUPkWcK8RVqOn+sED0puVDqRjXM+lj+NgQBYAzNq3nsqkHbEfcgQQ5wx1EFjmH2h1 YEyrIAjWtfxhNKIG2Uu35WtO1sEInXTsIaOYSB6jSR4pJKN/GaAzg337ZlCaC3rjAaM8 /5ZPlszlUWwRkfIgXiqcUNsTnjWV2pSzWScidpUl2qebJ1M19M4DOPhZnNrE8hmvO+dr MiqHZrAzDjzXsboPopWlr8KgRt5UKtVE23mGGFgmXzof09JyPFttrAwbPpMwLRk4t5Yz hDm5Ox3SCsWVUpdv98PdVCDekLiUmlnJLfMkrb3f8jUWHbcHWwuxkpbwqJHSN/3IDvth FE0g== X-Gm-Message-State: AD7BkJLhYBvOBLp5GSGT5+rWTVO8itaev6cY9KDY5GgeuBOeeJvfXg75q77dcQQSU5GoIZujAKkRwq+nn02t5w== X-Received: by 10.107.19.157 with SMTP id 29mr10878034iot.11.1460125076771; Fri, 08 Apr 2016 07:17:56 -0700 (PDT) In-Reply-To: <570778ad$0$1587$c3e8da3$5496439d@news.astraweb.com> X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-Mailman-Original-Message-ID: X-Mailman-Original-References: <57064D0D.1030701@rece.vub.ac.be> <87r3egfqiv.fsf@elektro.pacujo.net> <570778ad$0$1587$c3e8da3$5496439d@news.astraweb.com> Xref: csiph.com comp.lang.python:106682 On Fri, Apr 8, 2016 at 3:23 AM, Steven D'Aprano wrote: > On Fri, 8 Apr 2016 06:34 pm, Marko Rauhamaa wrote: > >> Antoon Pardon : >> >>> In python2 descending the tree would only involve at most one >>> expensive comparison, because using cmp would codify that comparison >>> into an integer which would then be cheap to compare with 0. Now in >>> python3, I may need to do two expensive comparisons, because there is >>> no __cmp__ method, to make such a codefication. >> >> I think you should base your tree implementation on key.__lt__() only. >> Only compare keys using <, nothing else, ever. > > I believe that's how list.sort() and sorted() work: > > py> class Spam(object): > ... def __init__(self, n): > ... self.n = n > ... def __lt__(self, other): > ... return self.n < other.n > ... def __repr__(self): > ... return repr(self.n) > ... > py> L = [Spam(5), Spam(3), Spam(9), Spam(1), Spam(2)] > py> L > [5, 3, 9, 1, 2] > py> sorted(L) > [1, 2, 3, 5, 9] > > > as well as max() and min(). That's fine for those operations and probably insert, but how do you search an AVL tree for a specific key without also using __eq__?