Question: 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.

Answer: 
I would just keep it simple:

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

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

```