TBH I've found the numeric tower a questionable addition to Python's stdlib. For PEP 484 we decided not to use it (using the concrete types, int, float etc. instead). So I'm not excited about adding more like this, especially since essentially *everything* can be used in a Boolean context.

If your specific project has a specific style requirement for Booleans that would be helped by a Boolean ABC, maybe you should add it to your project and see how it works out, and after a few months report back here.

Finally. How were you planning to use this new ABC?

On Mon, Feb 12, 2018 at 1:41 AM, Sylvain MARIE <sylvain.marie@schneider-electric.com> wrote:

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__ = ()

 

    @abstractmethod

    def __bool__(self):

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

 

    @abstractmethod

    def __and__(self, other):

        """self & other"""

 

    @abstractmethod

    def __rand__(self, other):

        """other & self"""

 

    @abstractmethod

    def __xor__(self, other):

        """self ^ other"""

 

    @abstractmethod

    def __rxor__(self, other):

        """other ^ self"""

 

    @abstractmethod

    def __or__(self, other):

        """self | other"""

 

    @abstractmethod

    def __ror__(self, other):

        """other | self"""

 

    @abstractmethod

    def __invert__(self):

        """~self"""

 

 

# register bool and numpy bool_ as virtual subclasses

# so that issubclass(bool, Boolean) = issubclass(np.bool_, Boolean) = True

Boolean.register(bool)

 

try:

    import numpy as np

    Boolean.register(np.bool_)

except ImportError:

    # silently escape

    pass

 

---------------------------

 

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

 

Sylvain

 


_______________________________________________
Python-ideas mailing list
Python-ideas@python.org
https://mail.python.org/mailman/listinfo/python-ideas
Code of Conduct: http://python.org/psf/codeofconduct/




--
--Guido van Rossum (python.org/~guido)