Reduce the overhead in functools.total_ordering by localizing NotImplemented.

(Sugguested by Serhiy Storchaka)
This commit is contained in:
Raymond Hettinger 2015-05-12 21:26:37 -07:00
parent 276e9c841d
commit ffcd8490d0
1 changed files with 12 additions and 12 deletions

View File

@ -94,80 +94,80 @@ def wraps(wrapped,
# infinite recursion that could occur when the operator dispatch logic # infinite recursion that could occur when the operator dispatch logic
# detects a NotImplemented result and then calls a reflected method. # detects a NotImplemented result and then calls a reflected method.
def _gt_from_lt(self, other): def _gt_from_lt(self, other, NotImplemented=NotImplemented):
'Return a > b. Computed by @total_ordering from (not a < b) and (a != b).' 'Return a > b. Computed by @total_ordering from (not a < b) and (a != b).'
op_result = self.__lt__(other) op_result = self.__lt__(other)
if op_result is NotImplemented: if op_result is NotImplemented:
return op_result return op_result
return not op_result and self != other return not op_result and self != other
def _le_from_lt(self, other): def _le_from_lt(self, other, NotImplemented=NotImplemented):
'Return a <= b. Computed by @total_ordering from (a < b) or (a == b).' 'Return a <= b. Computed by @total_ordering from (a < b) or (a == b).'
op_result = self.__lt__(other) op_result = self.__lt__(other)
return op_result or self == other return op_result or self == other
def _ge_from_lt(self, other): def _ge_from_lt(self, other, NotImplemented=NotImplemented):
'Return a >= b. Computed by @total_ordering from (not a < b).' 'Return a >= b. Computed by @total_ordering from (not a < b).'
op_result = self.__lt__(other) op_result = self.__lt__(other)
if op_result is NotImplemented: if op_result is NotImplemented:
return op_result return op_result
return not op_result return not op_result
def _ge_from_le(self, other): def _ge_from_le(self, other, NotImplemented=NotImplemented):
'Return a >= b. Computed by @total_ordering from (not a <= b) or (a == b).' 'Return a >= b. Computed by @total_ordering from (not a <= b) or (a == b).'
op_result = self.__le__(other) op_result = self.__le__(other)
if op_result is NotImplemented: if op_result is NotImplemented:
return op_result return op_result
return not op_result or self == other return not op_result or self == other
def _lt_from_le(self, other): def _lt_from_le(self, other, NotImplemented=NotImplemented):
'Return a < b. Computed by @total_ordering from (a <= b) and (a != b).' 'Return a < b. Computed by @total_ordering from (a <= b) and (a != b).'
op_result = self.__le__(other) op_result = self.__le__(other)
if op_result is NotImplemented: if op_result is NotImplemented:
return op_result return op_result
return op_result and self != other return op_result and self != other
def _gt_from_le(self, other): def _gt_from_le(self, other, NotImplemented=NotImplemented):
'Return a > b. Computed by @total_ordering from (not a <= b).' 'Return a > b. Computed by @total_ordering from (not a <= b).'
op_result = self.__le__(other) op_result = self.__le__(other)
if op_result is NotImplemented: if op_result is NotImplemented:
return op_result return op_result
return not op_result return not op_result
def _lt_from_gt(self, other): def _lt_from_gt(self, other, NotImplemented=NotImplemented):
'Return a < b. Computed by @total_ordering from (not a > b) and (a != b).' 'Return a < b. Computed by @total_ordering from (not a > b) and (a != b).'
op_result = self.__gt__(other) op_result = self.__gt__(other)
if op_result is NotImplemented: if op_result is NotImplemented:
return op_result return op_result
return not op_result and self != other return not op_result and self != other
def _ge_from_gt(self, other): def _ge_from_gt(self, other, NotImplemented=NotImplemented):
'Return a >= b. Computed by @total_ordering from (a > b) or (a == b).' 'Return a >= b. Computed by @total_ordering from (a > b) or (a == b).'
op_result = self.__gt__(other) op_result = self.__gt__(other)
return op_result or self == other return op_result or self == other
def _le_from_gt(self, other): def _le_from_gt(self, other, NotImplemented=NotImplemented):
'Return a <= b. Computed by @total_ordering from (not a > b).' 'Return a <= b. Computed by @total_ordering from (not a > b).'
op_result = self.__gt__(other) op_result = self.__gt__(other)
if op_result is NotImplemented: if op_result is NotImplemented:
return op_result return op_result
return not op_result return not op_result
def _le_from_ge(self, other): def _le_from_ge(self, other, NotImplemented=NotImplemented):
'Return a <= b. Computed by @total_ordering from (not a >= b) or (a == b).' 'Return a <= b. Computed by @total_ordering from (not a >= b) or (a == b).'
op_result = self.__ge__(other) op_result = self.__ge__(other)
if op_result is NotImplemented: if op_result is NotImplemented:
return op_result return op_result
return not op_result or self == other return not op_result or self == other
def _gt_from_ge(self, other): def _gt_from_ge(self, other, NotImplemented=NotImplemented):
'Return a > b. Computed by @total_ordering from (a >= b) and (a != b).' 'Return a > b. Computed by @total_ordering from (a >= b) and (a != b).'
op_result = self.__ge__(other) op_result = self.__ge__(other)
if op_result is NotImplemented: if op_result is NotImplemented:
return op_result return op_result
return op_result and self != other return op_result and self != other
def _lt_from_ge(self, other): def _lt_from_ge(self, other, NotImplemented=NotImplemented):
'Return a < b. Computed by @total_ordering from (not a >= b).' 'Return a < b. Computed by @total_ordering from (not a >= b).'
op_result = self.__ge__(other) op_result = self.__ge__(other)
if op_result is NotImplemented: if op_result is NotImplemented: