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


Groups > comp.lang.python > #101335

Re: True/False value testing

From Chris Angelico <rosuav@gmail.com>
Newsgroups comp.lang.python
Subject Re: True/False value testing
Date 2016-01-07 22:50 +1100
Message-ID <mailman.44.1452167431.2305.python-list@python.org> (permalink)
References <568e3fb4$0$659$426a74cc@news.free.fr> <mailman.43.1452163794.2305.python-list@python.org> <877fjl7i30.fsf@elektro.pacujo.net>

Show all headers | View raw


On Thu, Jan 7, 2016 at 10:19 PM, Marko Rauhamaa <marko@pacujo.net> wrote:
> Chris Angelico <rosuav@gmail.com>:
>
>> However, none of these will compare *equal* to the Boolean values True
>> and False, save for the integers 1 and 0. In fact, True is a special
>> form of the integer 1, and False is a special form of the integer 0;
>
> Stirring the pot:
>
>    >>> (2 < 3) is True
>    True
>
> but is that guaranteed?

Yes. When you do comparisons involving integers, the result is an
actual boolean value - not just a truthy value, but the exact value
True or False. Those values, being singletons (or a doubleton, or
whatever you want to call it), will always be the same objects, ergo
the "is True" part must always pass.

However, if arbitrary objects are thrown into the mix, the guarantee
doesn't hold. Here's one way of implementing a "sticky NaN" that
infects all operations:

class NaN:
    def __lt__(self, other): return self
    __gt__ = __ge__ = __le__ = __lt__
    def __eq__(self, other): return False
    def __ne__(self, other): return True
    def __new__(cls):
        if not hasattr(cls, "_inst"):
            cls._inst = super().__new__(cls)
        return cls._inst
    def __repr__(self): return "NaN"

NaN = NaN()


And it looks something like this:

>>> NaN < 1
NaN
>>> 1 < NaN
NaN
>>> NaN > 1
NaN
>>> 1 > NaN
NaN
>>> NaN == NaN
False

Similarly, numpy arrays overload operators to perform elementwise comparisons:

>>> import numpy
>>> numpy.array(range(10))
array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
>>> _ < 5
array([ True,  True,  True,  True,  True, False, False, False, False,
False], dtype=bool)

But with integers, and with most other built-in types, the comparison
operators are indeed guaranteed to return either True or False.

ChrisA

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


Thread

True/False value testing "ast" <nomail@invalid.com> - 2016-01-07 11:36 +0100
  Re: True/False value testing Peter Otten <__peter__@web.de> - 2016-01-07 11:48 +0100
  Re: True/False value testing Chris Angelico <rosuav@gmail.com> - 2016-01-07 21:49 +1100
    Re: True/False value testing Marko Rauhamaa <marko@pacujo.net> - 2016-01-07 13:19 +0200
      Re: True/False value testing Chris Angelico <rosuav@gmail.com> - 2016-01-07 22:50 +1100
        Re: True/False value testing Marko Rauhamaa <marko@pacujo.net> - 2016-01-07 14:07 +0200
          Re: True/False value testing Chris Angelico <rosuav@gmail.com> - 2016-01-07 23:38 +1100
  Re: True/False value testing "ast" <nomail@invalid.com> - 2016-01-07 12:11 +0100

csiph-web