Home > Software engineering >  Is there a simple way to compare two class objects for all items that are not none?
Is there a simple way to compare two class objects for all items that are not none?

Time:05-15

I am looking to compare two instances of the same class, but only the items for which both are not None.

for instance,

I will have a Bolt class, and one instance will have:

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

In this case, I want to compare these classes as true.

I know I can use the __dict__ of each class to iterate through the attributes, but I was wondering if this is something that could be a list comprehension.

CodePudding user response:

I would just keep it simple:

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

CodePudding user response:

I haven't got exactly the requirments for equality but I think that attrgetter from operator may be useful for a comparison of instances in a loop. Here an example of usage:

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))
  • Related