Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.001 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'static': 0.04; 'syntax': 0.04; 'error:': 0.07; 'string': 0.09; '2.3,': 0.09; 'attributes': 0.09; 'method,': 0.09; 'try:': 0.09; 'cc:addr:python-list': 0.11; 'python': 0.11; 'def': 0.12; '23,': 0.16; 'exception?': 0.16; 'from:addr:rosuav': 0.16; 'from:name:chris angelico': 0.16; 'keyerror': 0.16; 'skip:} 10': 0.16; 'subject:type': 0.16; '(you': 0.16; 'wrote:': 0.18; 'obviously': 0.18; 'cc:addr:python.org': 0.22; 'replace': 0.24; 'versions': 0.24; 'cc:2**0': 0.24; 'define': 0.26; 'possibly': 0.26; 'header:In-Reply-To:1': 0.27; 'function': 0.29; 'am,': 0.29; '2.3': 0.30; 'dec': 0.30; 'skip:@ 10': 0.30; 'subject:list': 0.30; 'message-id:@mail.gmail.com': 0.30; 'code': 0.31; 'block,': 0.31; 'class': 0.32; 'run': 0.32; 'running': 0.33; 'problem': 0.35; 'except': 0.35; 'but': 0.35; 'received:google.com': 0.35; 'raising': 0.36; 'should': 0.36; 'so,': 0.37; 'list.': 0.37; 'does': 0.39; 'sure': 0.39; 'how': 0.40; 'even': 0.60; 'above,': 0.60; 'is.': 0.60; 'simple': 0.61; 'invalid': 0.68; 'safe': 0.72; 'laid': 0.84; 'to:none': 0.92 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:date:message-id:subject:from:cc :content-type; bh=Mmq6DSdE2fvgEzQcUcbbk9ig3fRU2qqQXEmE/KoqHKc=; b=McEoT2j5eVHVpi/ZfuSQ7DA17Vjn9tpJHo840ZfcxYBwudMhUkJL1anPPCO8W1VJ19 /6+D82W6xaMdgS9Mbq7VUwxmPzMV/n3k/Lwsu/RJ+4ML7C0uzU22HqRRGt3mXy5E+2T9 xSYyU49S8xAphfAK6aXblr598J7hwg1A2mby1tK6sDK8WQVZCZYRkkqJCE8cg6NXhVqU c1PjMxh8KYp4+O3+mhr+8WC/Saky+awydTjmFfennkuAHLmX1poW7i0IGXv120zVSyQk PBTSBJVRr4ANiMJgWC3JiayoUn19hLoTttBpI/nllXSHvUwQbUvNwGVZlxFir+4vYcPa XX6w== MIME-Version: 1.0 X-Received: by 10.107.7.94 with SMTP id 91mr22487984ioh.27.1419290590324; Mon, 22 Dec 2014 15:23:10 -0800 (PST) In-Reply-To: <4747f671-8fd3-4b80-9108-e4888acc5f5b@googlegroups.com> References: <4747f671-8fd3-4b80-9108-e4888acc5f5b@googlegroups.com> Date: Tue, 23 Dec 2014 10:23:10 +1100 Subject: Re: TypeError: unhashable type: 'list' From: Chris Angelico Cc: "python-list@python.org" Content-Type: text/plain; charset=UTF-8 X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 37 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1419290593 news.xs4all.nl 2837 [2001:888:2000:d::a6]:56678 X-Complaints-To: abuse@xs4all.nl Path: csiph.com!usenet.pasdenom.info!bete-des-vosges.org!feed.ac-versailles.fr!news.ecp.fr!feeder1.cambriumusenet.nl!feed.tweaknews.nl!194.109.133.87.MISMATCH!newsfeed.xs4all.nl!newsfeed1.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail Xref: csiph.com comp.lang.python:82815 On Tue, Dec 23, 2014 at 10:10 AM, 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