I have a class that I want to be able to do bitwise operations on in some cases.
class Measurement(object):
def __init__(self, value = None, category = None, measure = None):
self.value = value
self.category = category
self.measure = measure
def __nonzero__(self):
return self.value.__nonzero__()
def __or__(self, other):
return self.__nonzero__() | other
a = False
b = Measurement(True)
At this point c = b | a works, but c = a | b gives a type error. How do I get it to work both ways?
Also, is there a more elegant way to do this; for example, I had hoped that by just defining __nonzero__, I might get the right behaviour, but that's not the case.
I'm using Python 2.7.
__rmul__(and make it call__mul__with the arguments switched). I wonder if there's a similar solution for this.