Message312416
This issue is created following the discussion [Python-ideas] Boolean ABC similar to what's provided in the 'numbers' module.
The following items are suggested:
- adding a 'Boolean' ABC class to the 'numbers' module
- register python 'bool' as a virtual subclass of both 'Boolean' and 'Integral'
- register numpy bool ('np.bool_') as a virtual subclass of 'Boolean' only
- rename 'Integral' 'Integer' and leave 'Integral' as an alias for backwards compatibility
Below is a proposal Boolean class:
---------------------
class Boolean(metaclass=ABCMeta):
"""
An abstract base class for booleans.
"""
__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
# bool is also a virtual subclass of Integral. np.bool_ is not.
Integral.register(bool) |
|
Date |
User |
Action |
Args |
2018-02-20 13:32:01 | smarie | set | recipients:
+ smarie |
2018-02-20 13:32:01 | smarie | set | messageid: <1519133521.3.0.467229070634.issue32886@psf.upfronthosting.co.za> |
2018-02-20 13:32:01 | smarie | link | issue32886 messages |
2018-02-20 13:32:01 | smarie | create | |
|