Given the following class:
import operator
class Foo(object):
def __init__(self, bah):
self.bah = bah
def __invert__(self):
return {'not': self.bah}
def __repr__(self):
return self.bah
def __or__(self, other):
return {'or': [self.bah, other]}
x = Foo('obj1')
y = Foo('obj2')
I can do:
operator.inv(x) # ~x
which gives me:
{'not': 'obj1'}
I can do:
operator.or_(x, ~y) # x | ~y
which gives me:
{'or': ['obj1', {'not': 'obj2'}]}
But why I cannot do:
operator.or_(~x, y) # ~x | y
which throws the following error:
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-175-92fdd35dc3b3> in <module>
----> 1 operator.or_(~x, y)
TypeError: unsupported operand type(s) for |: 'dict' and 'Foo'
And how would I be able to output the following?
{'or': [{'not': 'obj1'}, 'obj2']}