有没有一种简单的方法可以比较所有不是 none 的项目的两个 class 对象?
Is there a simple way to compare two class objects for all items that are not none?
我想比较相同 class 的两个实例,但只比较两者都不是 None
的项目。
例如,
我将有一个 Bolt
class,一个实例将有:
bolt1.locx = 1.0
bolt1.locy = 2.0
bolt1.locz = 3.0
bolt1.rpid = 1234
bolt1.nsid = [1,2,3,4]
bolt2.locx = 1.0
bolt2.locy = 2.0
bolt2.locz = 3.0
bolt2.rpid = None
bolt2.nsid = None
在这种情况下,我想比较这些 class 是否为真。
我知道我可以使用每个 class 的 __dict__
来遍历属性,但我想知道这是否可以作为列表理解。
我会保持简单:
class Bolt:
# ...
def __eq__(self, other):
if [self.locx, self.locy, self.locz] != [other.locx, other.locy, other.locz]:
return False
if self.rpid is not None && other.rpid is not None && self.rpid != other.rpid:
return False
if self.nsid is not None && other.nsid is not None && self.nsid != other.nsid:
return False
return True
我还没有完全了解平等的要求,但我认为
operator
中的 attrgetter
可能对 循环中的实例比较有用 。这是一个用法示例:
from operator import attrgetter
attrs = ('locx', 'locx', 'locz', 'rpid', 'nsid')
checks = []
for attr in attrs:
attr1 = attrgetter(attr)(bolt1)
attr2 = attrgetter(attr)(bolt2)
if attr1 is None and attr2 is None:
continue
checks.append(attr1 == attr2)
print(all(checks))
这是一种更通用的方法,不需要对属性比较进行硬编码
class Bolt:
def __eq__(self, other):
# check if the objects have a different set of attributes
if self.__dict__.keys() != other.__dict__.keys():
return False
for attr, self_attr in self.__dict__.items():
other_attr = getattr(other, attr)
if (self_attr is None) or (other_attr is None):
continue
elif self_attr != other_attr:
return False
return True
bolt1 = Bolt()
bolt2 = Bolt()
bolt1.locx = 1.0
bolt1.locy = 2.0
bolt1.locz = 3.0
bolt1.rpid = 1234
bolt1.nsid = [1,2,3,4]
bolt2.locx = 1.0
bolt2.locy = 2.0
bolt2.locz = 3.0
bolt2.rpid = None
bolt2.nsid = None
输出:
>>> bolt1.__dict__
{'locx': 1.0, 'locy': 2.0, 'locz': 3.0, 'rpid': 1234, 'nsid': [1, 2, 3, 4]}
>>> bolt2.__dict__
{'locx': 1.0, 'locy': 2.0, 'locz': 3.0, 'rpid': None, 'nsid': None}
>>> bolt1 == bolt2
True
>>> bolt2.rpid = 1
>>> bolt2.__dict__
{'locx': 1.0, 'locy': 2.0, 'locz': 3.0, 'rpid': 1, 'nsid': None}
>>> bolt1 == bolt2
False # different 'rpid' attribute values
>>> bolt2.rpid = None
>>> bolt2.extra_attr = 2
>>> bolt2.__dict__
{'locx': 1.0, 'locy': 2.0, 'locz': 3.0, 'rpid': None, 'nsid': None, 'extra_attr': 2}
>>> bolt1 == bolt2
False # different set of attributes
我想比较相同 class 的两个实例,但只比较两者都不是 None
的项目。
例如,
我将有一个 Bolt
class,一个实例将有:
bolt1.locx = 1.0
bolt1.locy = 2.0
bolt1.locz = 3.0
bolt1.rpid = 1234
bolt1.nsid = [1,2,3,4]
bolt2.locx = 1.0
bolt2.locy = 2.0
bolt2.locz = 3.0
bolt2.rpid = None
bolt2.nsid = None
在这种情况下,我想比较这些 class 是否为真。
我知道我可以使用每个 class 的 __dict__
来遍历属性,但我想知道这是否可以作为列表理解。
我会保持简单:
class Bolt:
# ...
def __eq__(self, other):
if [self.locx, self.locy, self.locz] != [other.locx, other.locy, other.locz]:
return False
if self.rpid is not None && other.rpid is not None && self.rpid != other.rpid:
return False
if self.nsid is not None && other.nsid is not None && self.nsid != other.nsid:
return False
return True
我还没有完全了解平等的要求,但我认为
operator
中的 attrgetter
可能对 循环中的实例比较有用 。这是一个用法示例:
from operator import attrgetter
attrs = ('locx', 'locx', 'locz', 'rpid', 'nsid')
checks = []
for attr in attrs:
attr1 = attrgetter(attr)(bolt1)
attr2 = attrgetter(attr)(bolt2)
if attr1 is None and attr2 is None:
continue
checks.append(attr1 == attr2)
print(all(checks))
这是一种更通用的方法,不需要对属性比较进行硬编码
class Bolt:
def __eq__(self, other):
# check if the objects have a different set of attributes
if self.__dict__.keys() != other.__dict__.keys():
return False
for attr, self_attr in self.__dict__.items():
other_attr = getattr(other, attr)
if (self_attr is None) or (other_attr is None):
continue
elif self_attr != other_attr:
return False
return True
bolt1 = Bolt()
bolt2 = Bolt()
bolt1.locx = 1.0
bolt1.locy = 2.0
bolt1.locz = 3.0
bolt1.rpid = 1234
bolt1.nsid = [1,2,3,4]
bolt2.locx = 1.0
bolt2.locy = 2.0
bolt2.locz = 3.0
bolt2.rpid = None
bolt2.nsid = None
输出:
>>> bolt1.__dict__
{'locx': 1.0, 'locy': 2.0, 'locz': 3.0, 'rpid': 1234, 'nsid': [1, 2, 3, 4]}
>>> bolt2.__dict__
{'locx': 1.0, 'locy': 2.0, 'locz': 3.0, 'rpid': None, 'nsid': None}
>>> bolt1 == bolt2
True
>>> bolt2.rpid = 1
>>> bolt2.__dict__
{'locx': 1.0, 'locy': 2.0, 'locz': 3.0, 'rpid': 1, 'nsid': None}
>>> bolt1 == bolt2
False # different 'rpid' attribute values
>>> bolt2.rpid = None
>>> bolt2.extra_attr = 2
>>> bolt2.__dict__
{'locx': 1.0, 'locy': 2.0, 'locz': 3.0, 'rpid': None, 'nsid': None, 'extra_attr': 2}
>>> bolt1 == bolt2
False # different set of attributes