openmc icon indicating copy to clipboard operation
openmc copied to clipboard

Consistency of __eq__ and __hash__

Open paulromano opened this issue 7 years ago • 1 comments

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

paulromano avatar May 05 '17 17:05 paulromano

This was mostly resolved with #946. We still need to do something about filters, but all the other classes are well behaved now.

paulromano avatar Dec 20 '17 07:12 paulromano