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

Addresses issue #827.
This commit is contained in:
Damien George 2014-08-26 09:31:26 +01:00
parent fa1a9bc9fd
commit 779794a680
3 changed files with 41 additions and 5 deletions

View File

@ -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__,
/*

View File

@ -64,6 +64,10 @@ Q(__getattr__)
Q(__del__)
Q(__call__)
Q(__lt__)
Q(__gt__)
Q(__eq__)
Q(__le__)
Q(__ge__)
Q(micropython)
Q(bytecode)

View File

@ -0,0 +1,31 @@
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))