openmc
openmc copied to clipboard
Consistency of __eq__ and __hash__
Python mandates that objects which compare equal have the same hash value. We are currently violating this in a number of our classes. For example, the Tally
class has an __eq__
method which doesn't account for its id
but a __hash__
method that does. This can lead to some unexpected behavior, like the following:
>>> import openmc
>>> t = openmc.Tally(1)
>>> d = {t: 10}
>>> t.id = 2
>>> d[t]
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
KeyError: Tally
ID = 2
Name =
Filters =
Nuclides =
Scores = []
Estimator = None
This was mostly resolved with #946. We still need to do something about filters, but all the other classes are well behaved now.