tricha122
tricha122

Reputation: 13

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

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.

Upvotes: 0

Views: 60

Answers (3)

Rodalm
Rodalm

Reputation: 5433

Here is a more generic way of doing it that doesn't require to hardcode the attribute comparisons

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

Output:

>>> 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

Upvotes: 0

cards
cards

Reputation: 4975

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

Upvotes: 0

Alexander
Alexander

Reputation: 63264

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

Upvotes: 1

Related Questions