Note that there are some explanatory texts on larger screens.

plurals
  1. POPython: hash defined on class won't recognise tuple
    primarykey
    data
    text
    <p>I am trying to implement the hash definition suggested <a href="https://stackoverflow.com/questions/2909106/python-whats-a-correct-and-good-way-to-implement-hash">here</a> but I am getting an error when trying to use hash on the class. The class is defined as so:</p> <pre><code>class Metabolite: def __init__(self, name_in='', id_in='', synonyms_in=[], InChIKey_in='', formulae_in=[], charge_in='', ecs_in=[], mtk_in = [], mtb_in = '', mtm_in = '', mts_in = '', bkm_id_in = '', source_in = ''): self.name = name_in self.id = id_in self.formulae = formulae_in self.inchikey = InChIKey_in self.synonyms = synonyms_in self.charge = charge_in self.ecs = ecs_in self.mtb = mtb_in self.mtk = mtk_in self.mtm = mtm_in self.mts = mts_in self.bkm_id = bkm_id_in self.source = source_in def key(self): return self.id, self.inchikey, self.mts, self.mtk, self.mtb, self.mtk def __key(self): return tuple([self.id, self.inchikey, self.mts, self.mtk, self.mtb, self.mtk]) def __eq__(self, other): return self.__key() == other.__key() def __hash__(self): return hash(tuple(self.__key())) </code></pre> <p>However on creating an instance of this class and asking for a hash I get the following:</p> <pre><code>&gt;&gt;&gt; met = Metabolite('this_metabolite', '10002', 'AADBRHFDG') &gt;&gt;&gt; hash(met) --------------------------------------------------------------------------- TypeError Traceback (most recent call last) &lt;ipython-input-8-7941e6f25128&gt; in &lt;module&gt;() ----&gt; 1 hash(met) classes.py in __hash__(self) 27 28 def __hash__(self): ---&gt; 29 return hash(tuple(self.__key())) 30 31 TypeError: unhashable type: 'list' </code></pre> <p>despite my desperate attempts in the class definition to force the type to be a (hashable) tuple. I am new to classes in Python, any help would be greatly appreciated.</p> <p>When run in IDLE, the error comes up as:</p> <pre><code>Traceback (most recent call last): File "&lt;pyshell#3&gt;", line 1, in &lt;module&gt; hash(met) File "&lt;string&gt;", line 27, in __hash__ TypeError: unhashable type: 'list' </code></pre> <p>SOLVED: thanks to those who responded.</p> <p>A tuple containing an empty list will not work:</p> <pre><code>&gt;&gt;&gt; my_tuple = tuple(('a',[])) &gt;&gt;&gt; type(my_tuple) tuple &gt;&gt;&gt; hash(my_tuple) --------------------------------------------------------------------------- TypeError Traceback (most recent call last) &lt;ipython-input-28-f63af41cc75b&gt; in &lt;module&gt;() ----&gt; 1 hash(my_tuple) TypeError: unhashable type: 'list' </code></pre> <p>so I had to create a list of my variables before returning them in <code>__key()</code>:</p> <pre><code>def __key(self): key_list = [] key_list.append(self.id) key_list.append(self.inchikey) key_list.append(self.mts) key_list.append(self.mtb) for entry in self.mtk: key_list.append(entry) return tuple(key_list) </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload