Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #82815

Re: TypeError: unhashable type: 'list'

References <4747f671-8fd3-4b80-9108-e4888acc5f5b@googlegroups.com>
Date 2014-12-23 10:23 +1100
Subject Re: TypeError: unhashable type: 'list'
From Chris Angelico <rosuav@gmail.com>
Newsgroups comp.lang.python
Message-ID <mailman.17142.1419290593.18130.python-list@python.org> (permalink)

Show all headers | View raw


On Tue, Dec 23, 2014 at 10:10 AM,  <ronald.kevin.burton@gmail.com> wrote:
> My problem is that I am not sure what the problem is. I can check the type of 'self' which is an object and the string 'Furnace Whistle' is obviously not a list. The static function 'MeasureMaker.Code2Measure' is a simple factory:
>
> class MeasureMaker:
>
>     def Code2Measure(measure_code, core):
>         try:
>             return {
> ...
>                 'Furnace Whistle': FurnaceWhistle(core)
>             }[measure_code]
>         except KeyError as error:
>             return None
>     Code2Measure = staticmethod(Code2Measure)
>
> What is the 'list' that is in the exception? Or how do I find out?

Does your MeasureMaker class define a __hash__ method? If so, what's
its definition? Possibly that's using some attributes and one of those
is a list.

Incidentally, is this actually how your code is laid out? What Python
versions do you need to support? Unless this code has to run on 2.3 or
earlier, you can replace the staticmethod call with a decorator:

@staticmethod
def Code2Measure(measure_code, core):
    ... code as above, but without the reassignment at the end ...

And if you were running this on 2.3, the "except KeyError as error"
syntax would be invalid anyway, so this should be a safe change :)

(You don't even need that try/except block, actually. If you use the
dict's .get() method, it'll return None instead of raising KeyError,
which is exactly what you need here.)

ChrisA

Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

TypeError: unhashable type: 'list' ronald.kevin.burton@gmail.com - 2014-12-22 15:10 -0800
  Re: TypeError: unhashable type: 'list' Chris Angelico <rosuav@gmail.com> - 2014-12-23 10:23 +1100
  Re: TypeError: unhashable type: 'list' Dave Angel <davea@davea.name> - 2014-12-22 18:44 -0500

csiph-web