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


Groups > comp.lang.python > #83736

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

Path csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!feeder.erje.net!eu.feeder.erje.net!newsfeed.xs4all.nl!newsfeed1a.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail
Return-Path <ian.g.kelly@gmail.com>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.000
X-Spam-Evidence '*H*': 1.00; '*S*': 0.00; 'else:': 0.03; '"""': 0.07; 'class,': 0.07; 'encoded': 0.07; 'interpreter.': 0.07; 'rename': 0.07; 'builtin': 0.09; 'clean.': 0.09; 'false,': 0.09; 'messing': 0.09; 'override': 0.09; 'statistical': 0.09; 'try:': 0.09; 'python': 0.11; 'def': 0.12; 'jan': 0.12; 'itself.': 0.14; '(assuming': 0.16; '(same': 0.16; '@classmethod': 0.16; '[1].': 0.16; 'bool': 0.16; 'boolean': 0.16; 'class)': 0.16; 'cmp': 0.16; 'compare.': 0.16; 'compares': 0.16; 'comparison.': 0.16; 'evaluates': 0.16; 'function).': 0.16; 'interpreter,': 0.16; 'iterable,': 0.16; 'iterables': 0.16; 'left,': 0.16; 'magic': 0.16; 'metaclass': 0.16; 'new-style': 0.16; "object's": 0.16; 'old-style': 0.16; 'pythonic': 0.16; 'rather,': 0.16; 'reason?': 0.16; 'reasonably': 0.16; 'subclass': 0.16; 'subclassing': 0.16; 'tuple': 0.16; 'tuple,': 0.16; 'typeerror:': 0.16; 'do,': 0.16; 'wrote:': 0.18; 'value.': 0.19; 'meant': 0.20; 'seems': 0.21; '>>>': 0.22; '2.x': 0.24; 'math': 0.24; 'non': 0.24; 'looks': 0.24; '(or': 0.24; 'class.': 0.26; 'compare': 0.26; 'define': 0.26; 'skip:_ 20': 0.27; 'values': 0.27; 'header:In-Reply-To:1': 0.27; 'point': 0.28; '[1]': 0.29; 'generally': 0.29; 'thus': 0.29; "doesn't": 0.30; 'andrew': 0.30; 'compared': 0.30; 'mode': 0.30; 'relative': 0.30; 'skip:@ 10': 0.30; 'message-id:@mail.gmail.com': 0.30; "i'm": 0.30; 'work.': 0.31; 'code': 0.31; 'lines': 0.31; '13,': 0.31; 'fine,': 0.31; 'int,': 0.31; 'operators': 0.31; 'class': 0.32; 'another': 0.32; 'proceed': 0.33; 'skip:_ 10': 0.34; 'maybe': 0.34; 'could': 0.34; 'classes': 0.35; 'except': 0.35; 'skip:s 30': 0.35; 'something': 0.35; 'but': 0.35; 'received:google.com': 0.35; 'there': 0.35; 'really': 0.36; 'false': 0.36; 'instances': 0.36; 'object,': 0.36; 'done': 0.36; "didn't": 0.36; 'method': 0.36; 'shows': 0.36; 'being': 0.38; 'rich': 0.38; 'to:addr:python-list': 0.38; 'pm,': 0.38; 'itself': 0.39; 'sure': 0.39; 'to:addr:python.org': 0.39; 'called': 0.40; 'even': 0.60; 'full': 0.61; 'course': 0.61; "you're": 0.61; 'advanced': 0.63; 'information': 0.63; 'skip:n 10': 0.64; 'between': 0.67; 'revealed': 0.68; 'default': 0.69; 'special': 0.74; 'valued': 0.78; '2015': 0.84; 'actually,': 0.84; 'shadow': 0.84; 'uncertain': 0.84; 'directly.': 0.95
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:to :content-type; bh=gJIVU+opBoZBipir8wO4RnKqsAcPCaJViMaw8s8g3Vg=; b=F7D1zFpHYg4fk/U4JMLmh8Mp8wn5Fb/IavjEpO1zimuS6LoKEzoMe5kBGRBzEzdW3H N7HdzWa8ij7vFrVzxwInC/Rv3Ss8PIyZ+ouL102E7d6TAkrMqJZZWT2QxttWITVvyvQO cn4IRa2lGqJmWFNsefZWSNHB4gBAsw9kTL67kJOSVmeAEibnjJbQJrhusKnl17lHAoQ2 EsBhFAqr5oDFp2AjHaMIgpBMTvpaGWZuIhm4Fo/asOt/SUvl3xThlyViOtzrWbtSpqQs wi4FzfAq9g9DXXkGcluBEpIWL9Ma56gRpGLRtAJ67qpiT5MUcv2MWDi0+cUQlpnxOriJ OScw==
X-Received by 10.68.227.72 with SMTP id ry8mr3712032pbc.101.1421224316164; Wed, 14 Jan 2015 00:31:56 -0800 (PST)
MIME-Version 1.0
In-Reply-To <54B5B486.7080406@r3dsolutions.com>
References <54ABB88A.7070504@r3dsolutions.com> <54ABC52A.1050507@davea.name> <54ABE383.3020801@r3dsolutions.com> <CAPTjJmpktyHfghEr+cjPrhso7JgB4VfS4=5Gdi95gT0aFbSYGA@mail.gmail.com> <54AC97D9.4010504@r3dsolutions.com> <CAPTjJmooTK_+HVoeyeSrqBHN1=YjRYQ2kfDyQtL9JoA9SbTqsQ@mail.gmail.com> <54ACAA04.60801@r3dsolutions.com> <54ADC99F.3020405@stoneleaf.us> <54B44A64.7010105@r3dsolutions.com> <CAPTjJmr0Bqcyj=VBADVSthSZ+pYMXQeNC-j8qLC2xHAVLUmcgA@mail.gmail.com> <mailman.17650.1421114374.18130.python-list@python.org> <54b4aded$0$2738$c3e8da3$76491128@news.astraweb.com> <54B5B486.7080406@r3dsolutions.com>
From Ian Kelly <ian.g.kelly@gmail.com>
Date Wed, 14 Jan 2015 01:31:15 -0700
Subject Re: Comparisons and sorting of a numeric class....
To Python <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.17706.1421224325.18130.python-list@python.org> (permalink)
Lines 114
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1421224325 news.xs4all.nl 2960 [2001:888:2000:d::a6]:49473
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:83736

Show key headers only | View raw


On Tue, Jan 13, 2015 at 5:12 PM, Andrew Robinson
<andrew3@r3dsolutions.com> wrote:
> So -- even a cursory thought shows that the information could be encoded in
> a very few lines even without an instance of a subclass:
>
> class CAllFalse():
>     @classmethod
>     def __nonzero__(Kls): return False
>
> class CPartFalse():
>     @classmethod
>     def __nonzero__(Kls): return False

This doesn't actually work.

>>> class CAllFalse(object):
...   @classmethod
...   def __nonzero__(cls): return False
...
>>> bool(CAllFalse)
True

When Python evaluates the "truthiness" of an object, it looks up the
__nonzero__ method on the object's class, not on the object itself
(same goes for all the other special dunder methods). Since CAllFalse
is itself a class, its own class (or rather, metaclass) is type [1].
Thus there's no point in using @classmethod on one of these special
methods. If you really want to proceed with this, you would have to
override the metaclass and define its __nonzero__ to return False,
like so:

>>> class CAllFalseMeta(type):
...     def __nonzero__(self): return False
...
>>> class CAllFalse(object):
...     __metaclass__ = CAllFalseMeta
...
>>> bool(CAllFalse)
False

In this code CAllFalse is an instance of CAllFalseMeta, while also
being a class itself.  But I'm not sure what the point of this is,
when you could just make CAllFalse a normal class and create an
instance of it.

[1] Actually, its metaclass is only type if you inherit from object,
which you didn't do, although in Python 2.x it must be done
explicitly. If you don't inherit from object, you get what is known as
a "classic class" instead of a new-style class. There is no reason to
use the old-style classes unless you really know what you are doing.

> class cmp():
>     lex=( CAllFalse,  CPartFalse, True )
>     @staticmethod
>     def __cmp__( left, right ):
>         if type(left) == type(right): # advanced statistical compare
>             return cmp.lex.index( right ).__cmp__( cmp.lex.index( left ) )
>         else: # legacy bool compare...
>            return right.__nonzero__().__cmp__( left.__nonzero__() )

Magic methods are generally meant to be used by the interpreter, not
called directly. The Pythonic way to write that last line would have
been:

    return cmp(bool(right), bool(left))

(Assuming of course that you rename the class to something that
doesn't shadow the cmp builtin function). This also would have
revealed sooner that your __nonzero__ method isn't actually invoked by
the interpreter.

> A full implementation, that distinguishes between legacy and probabilistic
> interpretations for the math operators could be done in several ways;
> Here's one that I think is reasonably clean.
>
> class RBool(tuple):
>     """
>     Rich compare bool.
>     A tuple which contains a bool, and a relative certainty value.
>     Whenever a RBool is compared against a bool or single valued object
> tuple,
>     it will default to legacy mode and compare only against the bool
>     However, whenever compared against another RBool, or multi-element
> tuple,
>     it will do a full rich comparison.
>     """
>     def __new__(cls, data):return super(RBool,cls).__new__(cls,data)
>     def _legacy(self,other):
>         'Resize local data for proper legacy compares as needed.'
>         try:
>             if len(other)==1: return (self[0],)  # legacy compare in tuples.
>         except TypeError: return self[0] # non iterables are a legacy
> compare.
>         return tuple(self) # A full rich non-recursive compare is warranted
>     def __eq__(self,other):return self._legacy(other) == other
>     def __ne__(self,other):return self._legacy(other) != other
>     def __lt__(self,other):return self._legacy(other) <  other
>     def __le__(self,other):return self._legacy(other) <= other
>     def __gt__(self,other):return self._legacy(other) >  other
>     def __ge__(self,other):return self._legacy(other) >= other
>     # def __cmp__( #TODO:
>     # def __nonzero__( #TODO:
>
> AllTrue   = True
> PartTrue  = RBool((False,3))
> Uncertain = RBool((False,2))
> PartFalse = RBool((False,1))
> AllFalse  = RBool((False,0))

This seems fine, but I don't get why you're messing around with
subclassing tuples. Do you really want to have boolean values that are
iterable, indexable, etc. for no reason? Just create a normal class
(or maybe subclass int, to match the normal bool class) and let your
instances contain a tuple instead of being one.

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


Thread

Re: Comparisons and sorting of a numeric class.... Andrew Robinson <andrew3@r3dsolutions.com> - 2015-01-12 17:59 -0800
  Re: Comparisons and sorting of a numeric class.... Steven D'Aprano <steve@pearwood.info> - 2015-01-13 05:32 +0000
    Re: Comparisons and sorting of a numeric class.... Chris Angelico <rosuav@gmail.com> - 2015-01-13 17:13 +1100
    Re: Comparisons and sorting of a numeric class.... Terry Reedy <tjreedy@udel.edu> - 2015-01-13 05:49 -0500
      Re: Comparisons and sorting of a numeric class.... Marko Rauhamaa <marko@pacujo.net> - 2015-01-13 13:00 +0200
        Re: Comparisons and sorting of a numeric class.... Chris Angelico <rosuav@gmail.com> - 2015-01-13 22:20 +1100
        Re: Comparisons and sorting of a numeric class.... Ian Kelly <ian.g.kelly@gmail.com> - 2015-01-13 11:56 -0700
        Re: Comparisons and sorting of a numeric class.... Chris Angelico <rosuav@gmail.com> - 2015-01-14 06:02 +1100
    Re: Comparisons and sorting of a numeric class.... Chris Angelico <rosuav@gmail.com> - 2015-01-13 21:56 +1100
    Re: Comparisons and sorting of a numeric class.... Andrew Robinson <andrew3@r3dsolutions.com> - 2015-01-13 16:12 -0800
    Re: Comparisons and sorting of a numeric class.... Ian Kelly <ian.g.kelly@gmail.com> - 2015-01-14 01:31 -0700
    Re: Comparisons and sorting of a numeric class.... Ian Kelly <ian.g.kelly@gmail.com> - 2015-01-14 22:26 -0700
      Re: Comparisons and sorting of a numeric class.... Gregory Ewing <greg.ewing@canterbury.ac.nz> - 2015-01-16 22:23 +1300
    Re: Comparisons and sorting of a numeric class.... Andrew Robinson <andrew3@r3dsolutions.com> - 2015-01-14 23:23 -0800
      Re: Comparisons and sorting of a numeric class.... Steven D'Aprano <steve@pearwood.info> - 2015-01-15 08:41 +0000
        Re: Comparisons and sorting of a numeric class.... Andrew Robinson <andrew3@r3dsolutions.com> - 2015-01-15 17:45 -0800
          Re: Comparisons and sorting of a numeric class.... Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2015-01-16 18:33 +1100
          Re: Comparisons and sorting of a numeric class.... Gregory Ewing <greg.ewing@canterbury.ac.nz> - 2015-01-17 00:07 +1300
          Re: Comparisons and sorting of a numeric class.... Rustom Mody <rustompmody@gmail.com> - 2015-01-16 03:22 -0800
          Re: Comparisons and sorting of a numeric class.... Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2015-01-17 23:27 +1100
        Re: Comparisons and sorting of a numeric class.... Mark Lawrence <breamoreboy@yahoo.co.uk> - 2015-01-16 02:27 +0000
        Re: Comparisons and sorting of a numeric class.... Ian Kelly <ian.g.kelly@gmail.com> - 2015-01-15 22:01 -0700
        Re: Comparisons and sorting of a numeric class.... Chris Angelico <rosuav@gmail.com> - 2015-01-16 17:58 +1100
      Re: Comparisons and sorting of a numeric class.... Rob Gaddi <rgaddi@technologyhighland.invalid> - 2015-01-15 09:54 -0800
        Re: Comparisons and sorting of a numeric class.... Andrew Robinson <andrew3@r3dsolutions.com> - 2015-01-26 15:47 -0800
        Re: Comparisons and sorting of a numeric class.... Ian Kelly <ian.g.kelly@gmail.com> - 2015-01-26 18:50 -0700
    Re: Comparisons and sorting of a numeric class.... Ian Kelly <ian.g.kelly@gmail.com> - 2015-01-15 10:05 -0700
    Re: Comparisons and sorting of a numeric class.... Andrew Robinson <andrew3@r3dsolutions.com> - 2015-01-23 03:46 -0800
      Re: Comparisons and sorting of a numeric class.... Rustom Mody <rustompmody@gmail.com> - 2015-01-23 07:31 -0800
        Re: Comparisons and sorting of a numeric class.... Ian Kelly <ian.g.kelly@gmail.com> - 2015-01-23 09:50 -0700
          Re: Comparisons and sorting of a numeric class.... Rustom Mody <rustompmody@gmail.com> - 2015-01-23 09:03 -0800
            Re: Comparisons and sorting of a numeric class.... Chris Angelico <rosuav@gmail.com> - 2015-01-24 04:23 +1100
              Re: Comparisons and sorting of a numeric class.... Rustom Mody <rustompmody@gmail.com> - 2015-01-23 09:29 -0800
            Re: Comparisons and sorting of a numeric class.... Ian Kelly <ian.g.kelly@gmail.com> - 2015-01-23 11:04 -0700
              Re: Comparisons and sorting of a numeric class.... Rustom Mody <rustompmody@gmail.com> - 2015-01-23 19:13 -0800
              Re: Comparisons and sorting of a numeric class.... Rustom Mody <rustompmody@gmail.com> - 2015-01-23 19:21 -0800
            Re: Comparisons and sorting of a numeric class.... Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2015-01-24 16:01 +1100
              Re: Comparisons and sorting of a numeric class.... Chris Angelico <rosuav@gmail.com> - 2015-01-24 16:43 +1100
          Re: Comparisons and sorting of a numeric class.... Rustom Mody <rustompmody@gmail.com> - 2015-01-23 09:22 -0800
            Re: Comparisons and sorting of a numeric class.... Chris Angelico <rosuav@gmail.com> - 2015-01-24 04:37 +1100
              Re: Comparisons and sorting of a numeric class.... Rustom Mody <rustompmody@gmail.com> - 2015-01-23 09:45 -0800
                Re: Comparisons and sorting of a numeric class.... Chris Angelico <rosuav@gmail.com> - 2015-01-24 05:03 +1100
                Re: Comparisons and sorting of a numeric class.... Ian Kelly <ian.g.kelly@gmail.com> - 2015-01-23 11:13 -0700
      Re: Comparisons and sorting of a numeric class.... Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2015-01-24 19:27 +1100
    Re: Comparisons and sorting of a numeric class.... Chris Angelico <rosuav@gmail.com> - 2015-01-23 22:58 +1100
    Re: Comparisons and sorting of a numeric class.... Mark Lawrence <breamoreboy@yahoo.co.uk> - 2015-01-23 12:07 +0000
    Re: Comparisons and sorting of a numeric class.... Terry Reedy <tjreedy@udel.edu> - 2015-01-23 11:17 -0500
    Re: Comparisons and sorting of a numeric class.... Ian Kelly <ian.g.kelly@gmail.com> - 2015-01-23 11:00 -0700

csiph-web