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


Groups > comp.lang.python > #82813 > unrolled thread

TypeError: unhashable type: 'list'

Started byronald.kevin.burton@gmail.com
First post2014-12-22 15:10 -0800
Last post2014-12-22 18:44 -0500
Articles 3 — 3 participants

Back to article view | Back to comp.lang.python


Contents

  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

#82813 — TypeError: unhashable type: 'list'

Fromronald.kevin.burton@gmail.com
Date2014-12-22 15:10 -0800
SubjectTypeError: unhashable type: 'list'
Message-ID<4747f671-8fd3-4b80-9108-e4888acc5f5b@googlegroups.com>
I am getting an exception that the traceback looks like:

------------------------------------------------------------
Traceback (most recent call last):
  File "C:\Projects\QA\robot_20141103\resources\assessment_utilities.py", line 29, in create_new_project
    program = customer.add(program_code)
  File "C:\Projects\QA\robot_20141103\resources\assessment_customer.py", line 589, in add
    program = ProgramMaker.Code2Program(program_code, self)
  File "C:\Projects\QA\robot_20141103\resources\programmaker.py", line 25, in Code2Program
    'ASSESS_PPL' : PPL(customer)
  File "C:\Projects\QA\robot_20141103\resources\ppl.py", line 20, in __init__
    MeasureMaker.Code2Measure('Furnace Whistle', self)
TypeError: unhashable type: 'list'
------------------------------------------------------------	

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?

Thank you.

Kevin

[toc] | [next] | [standalone]


#82815

FromChris Angelico <rosuav@gmail.com>
Date2014-12-23 10:23 +1100
Message-ID<mailman.17142.1419290593.18130.python-list@python.org>
In reply to#82813
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

[toc] | [prev] | [next] | [standalone]


#82816

FromDave Angel <davea@davea.name>
Date2014-12-22 18:44 -0500
Message-ID<mailman.17143.1419291899.18130.python-list@python.org>
In reply to#82813
On 12/22/2014 06:10 PM, ronald.kevin.burton@gmail.com wrote:
> I am getting an exception that the traceback looks like:
>
> ------------------------------------------------------------
> Traceback (most recent call last):
>    File "C:\Projects\QA\robot_20141103\resources\assessment_utilities.py", line 29, in create_new_project
>      program = customer.add(program_code)
>    File "C:\Projects\QA\robot_20141103\resources\assessment_customer.py", line 589, in add
>      program = ProgramMaker.Code2Program(program_code, self)
>    File "C:\Projects\QA\robot_20141103\resources\programmaker.py", line 25, in Code2Program
>      'ASSESS_PPL' : PPL(customer)
>    File "C:\Projects\QA\robot_20141103\resources\ppl.py", line 20, in __init__
>      MeasureMaker.Code2Measure('Furnace Whistle', self)
> TypeError: unhashable type: 'list'
> ------------------------------------------------------------	
>
> 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?
>


I don't see enough pieces to tell the problem at all.  The two lowest 
levels of stack trace are on line 20 and 25, and you don't include 
either of those in your fragments.  Further, since many of your 
statements are multi-statement lines, the problem isn't necessarily 
showing in the stack trace, which only shows one line of the offending 
statement.

Finally, I suspect PPL is some form of alias, which you don't show 
either.  And the stack trace shows four different source files. 
Probably only the last two matter, but it'd be very useful to see the 
class/function definitions involved in their entirety, plus any "from 
xxx import yyy" type aliases that may be relevant.

Are you perhaps using Python 2.x ?  If so, you want to inherit from 
object.  I doubt that's your problem, but it's a missing clue.  Always 
state your Python version when describing a new problem.

BTW, using @staticmethod decorator is usually clearer than the way you 
have it here.  But that's no bug either.

-- 
DaveA

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web