Consider this Unknown class:
class Unknown:
def __add__(self, other):
return self
def __radd__(self, other):
return self
unknown = Unknown()
assert (unknown + 1) is unknown
assert (1 + unknown) is unknown
This also works for __mul__, __rmul__, etc.
However, for Boolean operators e.g.:
assert (unknown or True) is True
assert (unknown or False) is unknown
Attempts such as -
def __or__(self, other):
return self if not other else other
- or any other combination did not work for me.
Note that I am also unable to define __bool__(self) since it's return value isn't True nor False and returning NotImplemented is not allowed.
So my question: is it possible to override the or, and and operators so that it might return something other than True or False?
Edit: Thanks to @CoryKramer and @Martijn Peters comment below.
I was under the impression that __or__ is for the logical or operator. It is not. It is for the bitwise | and in fact there is no way to override the logical or operator.