[go: up one dir, main page]

Skip to content

`Atoms.__getitem__`returns a copy, and equivalence checks therefore fail

Consider the following code block:

>>> a = Atoms('N3', [(0, 0, 0), (1, 0, 0), (0, 0, 1)])
>>> a[0] == a[0]
False
>>> a[0] in a
False

These seem like odd results. The reason for these results it thatAtoms.__getitem__ returns a copy of the Atom, and Atom.__eq__ checks for equivalence the default way (i.e. via the id built-in method).

What is the proper way to check for equivalence? For example I think this will work:

def Atom.__eq__(self, other):
    return self.atoms == other.atoms and self.index == other.index

Does that work for all situations, or are there edge cases where doing something like this will fail?

I did not see documentation or an issue noting this already, but if I missed it please let me know.

Also, if you don't mind explaining, I am curious why Atoms.__getitem__ returns a copy of an Atom rather than the same object.

EDIT:

>>> id(a[0]) == id(a[0])
True

The above line means that Atom.__eq__ is not using id to check for equality, so I really don't understand what is going on now.