Commit 779794a6 authored by Damien George's avatar Damien George
Browse files

py: Add dispatch for user defined ==, >, <=, >=.

Addresses issue #827.
parent fa1a9bc9
......@@ -381,11 +381,12 @@ STATIC const qstr binary_op_method_name[] = {
MP_BINARY_OP_INPLACE_MODULO,
MP_BINARY_OP_INPLACE_POWER,*/
[MP_BINARY_OP_LESS] = MP_QSTR___lt__,
/*MP_BINARY_OP_MORE,
MP_BINARY_OP_EQUAL,
MP_BINARY_OP_LESS_EQUAL,
MP_BINARY_OP_MORE_EQUAL,
MP_BINARY_OP_NOT_EQUAL,
[MP_BINARY_OP_MORE] = MP_QSTR___gt__,
[MP_BINARY_OP_EQUAL] = MP_QSTR___eq__,
[MP_BINARY_OP_LESS_EQUAL] = MP_QSTR___le__,
[MP_BINARY_OP_MORE_EQUAL] = MP_QSTR___ge__,
/*
MP_BINARY_OP_NOT_EQUAL, // a != b calls a == b and inverts result
*/
[MP_BINARY_OP_IN] = MP_QSTR___contains__,
/*
......
......@@ -64,6 +64,10 @@ Q(__getattr__)
Q(__del__)
Q(__call__)
Q(__lt__)
Q(__gt__)
Q(__eq__)
Q(__le__)
Q(__ge__)
Q(micropython)
Q(bytecode)
......
class foo(object):
def __init__(self, value):
self.x = value
def __eq__(self, other):
print('eq')
return self.x == other.x
def __lt__(self, other):
print('lt')
return self.x < other.x
def __gt__(self, other):
print('gt')
return self.x > other.x
def __le__(self, other):
print('le')
return self.x <= other.x
def __ge__(self, other):
print('ge')
return self.x >= other.x
for i in range(3):
for j in range(3):
print(foo(i) == foo(j))
print(foo(i) < foo(j))
print(foo(i) > foo(j))
print(foo(i) <= foo(j))
print(foo(i) >= foo(j))
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment