Added hashing methods for sets/dictionaries.

This commit is contained in:
SimpleArt
2021-03-07 14:18:13 -05:00
parent d0b222ec85
commit e12287cdcd
2 changed files with 10 additions and 0 deletions

View File

@ -112,6 +112,11 @@ class Chromosome():
return self.gene_list == chromosome.gene_list
def __hash__(self):
"""Hash chromosomes by genes so they can be used in sets/dictionaries."""
return hash(tuple(self))
def __add__(self, chromosome):
"""Return self + chromosome, a chromosome made by concatenating the genes."""
return Chromosome(chain(self, chromosome))

View File

@ -19,6 +19,11 @@ class Gene:
return self.value == Gene(other_gene).value
def __hash__(self):
"""Hash genes by value so that they can be used in sets/dictionaries."""
return hash(self.value)
def __repr__(self):
"""
Allows the user to use