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


Groups > comp.lang.python > #83281

Re: Comparisons and sorting of a numeric class....

Path csiph.com!usenet.pasdenom.info!aioe.org!news.stack.nl!newsfeed.xs4all.nl!newsfeed4a.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!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.001
X-Spam-Evidence '*H*': 1.00; '*S*': 0.00; 'operator': 0.03; 'interpreter': 0.05; 'stops': 0.07; 'users,': 0.07; 'deemed': 0.09; 'expectation': 0.09; 'false,': 0.09; 'method,': 0.09; 'none)': 0.09; 'cc:addr:python-list': 0.11; 'python': 0.11; 'def': 0.12; 'jan': 0.12; 'assume': 0.14; 'random': 0.14; '2.7.3': 0.16; 'expects': 0.16; 'expression,': 0.16; 'from:addr:rosuav': 0.16; 'from:name:chris angelico': 0.16; 'mylist': 0.16; "object's": 0.16; 'order)': 0.16; 'parentheses': 0.16; 'parts,': 0.16; 'precedence': 0.16; 'returned,': 0.16; 'tuple,': 0.16; 'typeerror:': 0.16; 'x()': 0.16; 'elements': 0.16; 'wrote:': 0.18; 'wed,': 0.18; 'first.': 0.19; 'value.': 0.19; 'starts': 0.20; '>>>': 0.22; 'example': 0.22; 'separate': 0.22; 'cc:addr:python.org': 0.22; 'integer': 0.24; 'question': 0.24; 'cc:2**0': 0.24; 'sort': 0.25; 'class.': 0.26; 'define': 0.26; 'possibly': 0.26; 'tables': 0.26; 'second': 0.26; 'certain': 0.27; 'gets': 0.27; 'header:In-Reply-To:1': 0.27; 'function': 0.29; 'generally': 0.29; 'compared': 0.30; 'dec': 0.30; 'returned': 0.30; 'see,': 0.30; 'message-id:@mail.gmail.com': 0.30; 'skip:( 20': 0.30; "i'm": 0.30; 'usually': 0.31; '"",': 0.31; 'are.': 0.31; 'assumes': 0.31; 'comparison': 0.31; 'question:': 0.31; 'reflected': 0.31; "they'll": 0.31; 'allows': 0.31; 'file': 0.32; 'class': 0.32; '(including': 0.33; 'linux': 0.33; '(most': 0.33; 'skip:_ 10': 0.34; "i'd": 0.34; 'could': 0.34; 'knows': 0.35; 'something': 0.35; 'equal': 0.35; 'objects': 0.35; 'operations': 0.35; 'but': 0.35; 'received:google.com': 0.35; 'instances': 0.36; 'next': 0.36; 'method': 0.36; 'should': 0.36; 'two': 0.37; 'list': 0.37; 'being': 0.38; 'easiest': 0.38; 'pm,': 0.38; 'that,': 0.38; 'recent': 0.39; 'enough': 0.39; 'either': 0.39; 'skip:p 20': 0.39; 'called': 0.40; 'dave': 0.60; 'truly': 0.60; 'range': 0.61; 'first': 0.61; 'name': 0.63; 'skip:n 10': 0.64; 'more': 0.64; 'different': 0.65; 'here': 0.66; 'mar': 0.68; 'detail.': 0.68; 'special': 0.74; '2014,': 0.84; '2015': 0.84; "class's": 0.84; 'here...': 0.84; 'step,': 0.84; 'symptoms': 0.84; 'treating': 0.84; 'angel': 0.91; 'this;': 0.91; 'to:none': 0.92; 'average': 0.93
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:cc :content-type; bh=khkusg6G7wpGRxoQR7xCnwMGssfKOq7PmFC+2+VT0MI=; b=Zk9fuziShDEYkU4e4CPWlexiN2claI2Gq+9foQqauRMxGuaEbOwgV4SwSjfX1q50Z+ 8GSFmHqReSw94WZ8uF5KcvCPvLm3moYUZJYmMHkeX/OYvhGtK9c8H6HJ8KhaIboUjWlW Ez2rTyczsHeGlvPMyVsQw3obOYelIc/Y+2vCf5zZPmecUL4sJRPVKqinrC8QO/9+sVv1 hHe6ucCLfTvXHnxHVvFKZwqiIKNbfvS9gBhiV1iGTKKPhnjnE0ryFNcKy4UiiwZLWE6X 8N2dhoguc5WrwN8qTRZHEGZu6v4SZGzS2DRsYNUEgleYUnaDIR+IBiQt/pHFOYubb58T 7MXg==
MIME-Version 1.0
X-Received by 10.50.79.161 with SMTP id k1mr1591519igx.14.1420603288043; Tue, 06 Jan 2015 20:01:28 -0800 (PST)
In-Reply-To <54ACAA85.3040806@davea.name>
References <54ABB88A.7070504@r3dsolutions.com> <54ABC52A.1050507@davea.name> <54ABE383.3020801@r3dsolutions.com> <54ABEAD9.8070000@davea.name> <54AC938C.7040206@r3dsolutions.com> <54ACAA85.3040806@davea.name>
Date Wed, 7 Jan 2015 15:01:27 +1100
Subject Re: Comparisons and sorting of a numeric class....
From Chris Angelico <rosuav@gmail.com>
Cc "python-list@python.org" <python-list@python.org>
Content-Type text/plain; charset=UTF-8
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 <https://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 <https://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.17431.1420603297.18130.python-list@python.org> (permalink)
Lines 109
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1420603297 news.xs4all.nl 2842 [2001:888:2000:d::a6]:53898
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:83281

Show key headers only | View raw


On Wed, Jan 7, 2015 at 2:39 PM, Dave Angel <davea@davea.name> wrote:
> Here we need help from someone who knows more about this particular detail.
> I don't know which special methods bool() or not will call, but I'd think it
> would be something with a better name than __nonzero__

In Python 2, it is indeed __nonzero__, but in Python 3, it's __bool__:

Python 2.7.3 (default, Mar 13 2014, 11:03:55)
[GCC 4.7.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> class X:
...  def __nonzero__(self): print("called")
...
>>> bool(X())
called
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: __nonzero__ should return an int


Python 3.5.0a0 (default:1c51f1650c42+, Dec 29 2014, 02:29:06)
[GCC 4.7.2] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> class X:
...  def __bool__(self): print("called")
...
>>> bool(X())
called
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: __bool__ should return bool, returned NoneType


Also, as you can see, Py2's method allows any integer to be returned,
while Py3's expects a bool. The boolification of an object MUST result
in either True or False, nothing else.

>> Question:  If two different class's instances are being compared by
>> '==', and both define an __eq__ method, which method gets called?  ( I
>> don't know if that applied here... but I'm not familiar with order of
>> operations )
>
>
> The left object's methods are generally examined first.  So if you said
>     mylist * 5
>
> you'd be using a __mul__ method of list class.

Order of operations has nothing to do with this; that's to do with
operator precedence/associativity, which is what stipulates that all
of these parentheses are unnecessary:

a + (b * c)
(a + b) + c
a ** (b ** c)
a or (b and c)
(a + b) in c
a if (b + c) else d

The operator precedence tables neither know nor care what the objects
in question are. But once the interpreter gets to the next step, and
starts actually figuring out the value of the expression, then it
usually goes for the left object's methods first; if that object
returns NotImplemented, it'll try the right object (sometimes with a
reflected method):

>>> class X:
...  def __eq__(self, other): print("Am I equal to:",other)
...  def __add__(self, other): print("me +",other)
...  def __radd__(self, other): print(other,"+ me")
...
>>> X() == 1
Am I equal to: 1
>>> 2 == X()
Am I equal to: 2
>>> X() + 3
me + 3
>>> 4 + X()
4 + me

>> But I don't think that stops me from treating the sort order of all
>> items which are quasi-equal, as a second search (hierarchical search).
>> eg: Sort first by definite magnitude, then among those deemed 'equal',
>> sort them by average expectation value...  That's going to be good
>> enough for me and my users, for the sort order of truly quasi equal
>> things is arbitrary anyway... as long as it's not 'defintely wrong'
>> they'll have no reason to complain.
>
>
> You could write a sort that works that way, but I don't think you can assume
> that the built-in sort will.  sort assumes that the comparison works in a
> certain way, and if it doesn't, the symptoms could range from random
> ordering (including items drastically out of order) to a possibly never
> terminating sort.

If you can separate the value into two parts, magnitude and
expectation, you could have a key function that makes them sortable:

def mag_exp(val):
    if not isinstance(val, MagicFloat): return (val, None)
    return int(val), val-int(val) # example

srtlst = sorted(lst, key=mag_exp)

That'll sort all elements by the first value in the tuple, and if
they're identical on that, by the second value. That's the easiest way
to do a multi-part sort.

ChrisA

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


Thread

Re: Comparisons and sorting of a numeric class.... Chris Angelico <rosuav@gmail.com> - 2015-01-07 15:01 +1100

csiph-web