Add __ne__ built-in function

In Python 3 __ne__ by default delegates to __eq__ and inverts the
result, but in Python 2 they urge you to define __ne__ when you
define __eq__ for it to work properly [1].There are no implied
relationships among the comparison operators. The truth of x==y
does not imply that x!=y is false. Accordingly, when defining __eq__(),
one should also define __ne__() so that the operators will behave
as expected.
[1]https://docs.python.org/2/reference/datamodel.html#object.__ne__

Change-Id: I05f4ecbdc9d430f970a11b0451a7699d06f8a8ad
This commit is contained in:
gecong1973 2016-09-05 16:32:23 +08:00
parent a4c49fab02
commit 574a982d19
3 changed files with 9 additions and 0 deletions

View File

@ -68,6 +68,9 @@ class LFUCache(object):
else:
return self == other
def __ne__(self, other):
return not self.__eq__(other)
def __setitem__(self, item, value):
self.set(item, value)

View File

@ -34,5 +34,8 @@ class DBLayerProxy(wrapt.ObjectProxy):
return self.__wrapped__ == other
return self.__wrapped__ == other.__wrapped__
def __ne__(self, other):
return not self.__eq__(other)
def __repr__(self):
return "<P: %r>" % self.__wrapped__

View File

@ -1087,6 +1087,9 @@ class Task(Model):
return self.key == other
return self.key == other.key
def __ne__(self, other):
return not self.__eq__(other)
def __repr__(self):
return 'Task(execution={} name={})'.format(self.execution, self.name)