[Python-ideas] Boolean ABC similar to what's provided in the 'numbers' module

Sylvain MARIE sylvain.marie at schneider-electric.com
Mon Feb 12 04:41:04 EST 2018

The numbers module provides very useful ABC for the 'numeric tower', able to abstract away the differences between python primitives and for example numpy primitives.
I could not find any equivalent for Booleans.
However numpy defines np.bool too, so being able to have an abstract Boolean class for both python bool and numpy bool would be great.

Here is a version that I included in valid8 in the meantime

class Boolean(metaclass=ABCMeta):
    An abstract base class for booleans, similar to what is available in numbers
    see https://docs.python.org/3.5/library/numbers.html
    __slots__ = ()

    def __bool__(self):
        """Return a builtin bool instance. Called for bool(self)."""

    def __and__(self, other):
        """self & other"""

    def __rand__(self, other):
        """other & self"""

    def __xor__(self, other):
        """self ^ other"""

    def __rxor__(self, other):
        """other ^ self"""

    def __or__(self, other):
        """self | other"""

    def __ror__(self, other):
        """other | self"""

    def __invert__(self):

# register bool and numpy bool_ as virtual subclasses
# so that issubclass(bool, Boolean) = issubclass(np.bool_, Boolean) = True

    import numpy as np
except ImportError:
    # silently escape


If that topic was already discussed and settled in the past, please ignore this thread - apologies for not being able to find it.
Best regards


-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20180212/95ae8438/attachment.html>

More information about the Python-ideas mailing list