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: Ib9db2166313f49f3b40df7ce0aba8b01c6a2869a
This commit is contained in:
gecong1973 2016-09-05 16:40:44 +08:00
parent 90bb67ffb6
commit 371bf1bdff

View File

@ -36,6 +36,9 @@ class Entity(object):
other.name == self.name and
other.entity_type == self.entity_type)
def __ne__(self, other):
return not self.__eq__(other)
class Instance(Entity):
TYPE = "instance"
@ -56,6 +59,9 @@ class Instance(Entity):
other.os == self.os and
other.metadata == self.metadata)
def __ne__(self, other):
return not self.__eq__(other)
class OS(object):
def __init__(self, os_type, distro, version):
@ -68,6 +74,9 @@ class OS(object):
other.distro == self.distro and
other.version == self.version)
def __ne__(self, other):
return not self.__eq__(other)
class Volume(Entity):
TYPE = "volume"
@ -85,6 +94,9 @@ class Volume(Entity):
other.size == self.size and
other.attached_to == self.attached_to)
def __ne__(self, other):
return not self.__eq__(other)
class VolumeType(object):
def __init__(self, volume_type_id, volume_type_name):
@ -94,6 +106,9 @@ class VolumeType(object):
def __eq__(self, other):
return other.__dict__ == self.__dict__
def __ne__(self, other):
return not self.__eq__(other)
def as_dict(self):
return todict(self)