[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

On Wed, 22 Jul 2020 at 02:12, Chris Angelico <rosuav at gmail.com> wrote: > > On Wed, Jul 22, 2020 at 11:04 AM Tim Chase > <python.list at tim.thechases.com> wrote: > > > > I know for ints, cpython caches something like -127 to 255 where `is` > > works by happenstance based on the implementation but not the spec > > (so I don't use `is` for comparison there because it's not > > guaranteed by the language spec). On the other hand, I know that None > > is a single object that can (and often *should*) be compared using > > `is`. However I spent some time reading through the language specs and > > didn't encounter anything about booleans returned from > > comparisons-operators, guaranteeing that they always return The One > > True and The One False. ... > > That said, though, a comparison isn't required to return a bool. If it > *does* return a bool, it has to be one of those exact two, but it > could return anything it chooses. But for built-in types and most > user-defined types, you will indeed get a bool. I'm not sure if this is relevant to the question but thought I'd mention concrete examples. A numpy array will return non-bool for both of the mentioned operators: In [2]: import numpy as np In [3]: a = np.array([2, 2, 2]) In [4]: a == a Out[4]: array([ True, True, True]) In [5]: a > 4 Out[5]: array([False, False, False]) With sympy expressions == will strictly always return a bool but inequality operators can return instances of Relational. When they can be evaluated those will give sympy's Booleans rather than bool. In [6]: import sympy as sym In [7]: x = sym.Symbol('x') In [8]: x > 0 Out[8]: x > 0 In [9]: type(_) Out[9]: sympy.core.relational.StrictGreaterThan In [10]: (x > 0).subs(x, 2) Out[10]: True In [11]: type(_) Out[11]: sympy.logic.boolalg.BooleanTrue -- Oscar

- Prev by Date:
**Installing Python 3.8.3 with tkinter** - Next by Date:
**Python Program Help** - Previous by thread:
**True is True / False is False?** - Next by thread:
**True is True / False is False?** - Index(es):