I have some Python code that has a user type called Person. I have tried making either a set of these objects or a list of these objects. The surrounding code generally creates new instances of this object. Person defines
def __eq__(self, other):
return isinstance(other, Person) and self.handle == other.handle
handle will be the same for different objects that actually refer to the same person, so this will check whether the different instances should actually be regarded as the same.
With a list, I say:
if child not in child_list:
child_list.append(child)
and this does not produce duplicates.
With a set I use update(), and this produces duplicates (i.e. multiple occurrences where the handle is the same).
children = set(sdb.children(person))
if additional_parents:
for spouse in additional_parents:
for family in sdb.parent_in(spouse):
children.update(sdb.children(family))
This explanation says that set inclusion (and hence presumably update) tests the hash first (which is not defined for this user type, and hence is different for different instances of the user type).
HOWEVER, this Python documentation says that:
For container types such as list, tuple, set, frozenset, dict, or collections.deque, the expression
x in yis equivalent toany(x is e or x == e for e in y).
So I think that set append() should be using the same test as “not in” for lists (i.e. __eq__). Why does this not seem to be the case?