I've read most of the PEP 207 and other sources, and it confirms that the Python interpreter desugars a > b > c to a > b and b > c.
However, this happens even if the magic method for this operator is implemented by a custom class.
They mention __boolean_and__ however it seems to be something internal to C level code.
What I am looking for is a way to implement the operator as any other like + or * to be used in chains with multiple operands (without recurring to parentheses), but it seems that the comparison operators are somewhat in a special category, apart from the others.
The code bellow show an illustative example of the problem:
class A:
def __init__(self, n):
self.n = n
def __ge__(self, other):
return A(self.n + other.n)
def __repr__(self):
return self.n
print(A("a") >= A("b") >= A("c"))
# prints 'bc'
The intended output is 'abc'.
Is there any way to avoid the desugaring, or the conversion happens at the parser/interpreter level, making it impossible to change?
Just to make it clear, here (and in my real, more complex, problem), the operator is not used with the meaning of comparison in any sense (except as a prescription of what comes first, instead of a query about which operand is greater).