[Python-ideas] Boolean ABC similar to what's provided in the 'numbers' module
Guido van Rossum
guido at python.org
Mon Feb 12 11:08:14 EST 2018
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 at 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 at python.org
> https://mail.python.org/mailman/listinfo/python-ideas
> Code of Conduct: http://python.org/psf/codeofconduct/
>
>
--
--Guido van Rossum (python.org/~guido)
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20180212/0f549e7c/attachment.html>
More information about the Python-ideas
mailing list