Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9239323
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T07:57:40+00:00 2026-06-18T07:57:40+00:00

I am trying to implement the hash definition suggested here but I am getting

  • 0

I am trying to implement the hash definition suggested here but I am getting an error when trying to use hash on the class. The class is defined as so:

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()))

However on creating an instance of this class and asking for a hash I get the following:

>>> met = Metabolite('this_metabolite', '10002', 'AADBRHFDG')
>>> hash(met)

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-8-7941e6f25128> in <module>()
----> 1 hash(met)

classes.py in __hash__(self)
     27 
     28     def __hash__(self):
---> 29         return hash(tuple(self.__key()))
     30 
     31 

TypeError: unhashable type: 'list'

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.

When run in IDLE, the error comes up as:

Traceback (most recent call last):
  File "<pyshell#3>", line 1, in <module>
    hash(met)
  File "<string>", line 27, in __hash__
TypeError: unhashable type: 'list'

SOLVED: thanks to those who responded.

A tuple containing an empty list will not work:

>>> my_tuple = tuple(('a',[]))

>>> type(my_tuple)

tuple

>>> hash(my_tuple)

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-28-f63af41cc75b> in <module>()
----> 1 hash(my_tuple)

TypeError: unhashable type: 'list'

so I had to create a list of my variables before returning them in __key():

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)
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T07:57:42+00:00Added an answer on June 18, 2026 at 7:57 am

    The problem is that the mtk attribute is a list (by default). Mutable sequence types cannot be hashed and since your key contains mtk (twice), the key is not hashable (even if the key itself is a tuple).

    If you modify your key generation as follows, then it becomes hashable (but it might prove seriously inefficient):

    def __key(self):
        mtk = tuple(self.mtk)
        return tuple([self.id, self.inchikey, self.mts, mtk, self.mtb, mtk])
    

    This returns in Python 2.7 (code pasted from your question with above modification):

    >>> met = Metabolite('this_metabolite', '10002', 'AADBRHFDG')
    >>> hash(met)
    7276685348836095537
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to implement a Chord distributed hash table. I want to use SHA-1
I trying to implement a memoized_finder method and I am getting an error that
Trying to implement a search similar to here .This searches properties based on city,locality,property
I am new to boost. I am trying to implement boost::unorder_set. Here is the
I am trying to implement a hash table
I'm trying to implement an efficient hash table where collisions are solved using linear
Possible Duplicate: The fundamentals of Hash tables? I am trying to implement a Simple
I'm trying to implement a dictionary with a hash table (not using Java's provided
I'm trying to implement email confirmation using Pyramid framework. Here's the code that confirms
I am trying to implement the use of two HashMaps and it seems to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.