Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!feeder.erje.net!eu.feeder.erje.net!xlned.com!feeder3.xlned.com!newsfeed.xs4all.nl!newsfeed2.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.012 X-Spam-Evidence: '*H*': 0.98; '*S*': 0.00; 'pursuing': 0.07; '22,': 0.09; '[1,': 0.09; 'literal': 0.09; 'lst': 0.09; 'objects,': 0.09; 'def': 0.12; 'enlighten': 0.16; 'from:addr:rosuav': 0.16; 'from:name:chris angelico': 0.16; "object's": 0.16; 'typeerror:': 0.16; 'unhashable': 0.16; 'sat,': 0.16; 'wrote:': 0.18; '>>>': 0.22; 'second': 0.26; 'certain': 0.27; 'header:In-Reply-To:1': 0.27; '[1]': 0.29; 'am,': 0.29; 'waste': 0.30; 'message- id:@mail.gmail.com': 0.30; "i'm": 0.30; '"",': 0.31; 'file': 0.32; 'class': 0.32; 'another': 0.32; '(most': 0.33; 'skip:_ 10': 0.34; 'maybe': 0.34; 'johnson': 0.35; 'received:google.com': 0.35; 'work?': 0.38; 'to:addr:python-list': 0.38; 'anything': 0.39; 'recent': 0.39; 'to:addr:python.org': 0.39; 'free': 0.61; 'back': 0.62; 'pick': 0.64; 'more': 0.64; 'different': 0.65; 'subject:Value': 0.84; 'rick': 0.93; '2013': 0.98 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:to :content-type; bh=woZGEFFZL9QG3NvYL7JSmh0/zCG26p5Wa6LwcnlK4ME=; b=S0OB+aDAr3E0E/Umlh+yRAO1rsMnGOhfC2LVuaBLPmn8m5PZAWtDHSUgSoah41AOI+ muZ7Vej49tQu2gmlIvOUqAGO+pgQmjJXGDdNMc5ANzQ42TuXY0vtm8QRifJXeHaa0NeS /zMPo/yuqmTnHapimIGiVgMYRoRwOs8eIJqgI5zPc8B2OTlfVrCD0N2SXll2x7Di0qS0 GcYtu7eTt7KRAODm7bGdueKNDOaL16zB4uSQ4znzY4uLrwup+KjMRoINcRKCq6V7sMJE xOcqJhfi0VmYqTiDbrP/hFvEE5/dEMvp9BhM3UHopYjhapWMyUQACOigSO2fYBQPvArf dw/A== MIME-Version: 1.0 X-Received: by 10.220.169.146 with SMTP id z18mr6820451vcy.80.1371865061669; Fri, 21 Jun 2013 18:37:41 -0700 (PDT) In-Reply-To: <46567a60-bb75-4f22-b968-bf3ccbf35afb@googlegroups.com> References: <7e6361d5-6619-4aaa-adda-8b5f01bde57f@googlegroups.com> <447dd1c6-1bb2-4276-a109-78d7a067b442@d8g2000pbe.googlegroups.com> <2e92b4c7-31be-40d2-a906-ab19f3630dfa@googlegroups.com> <51c477dd$0$29999$c3e8da3$5496439d@news.astraweb.com> <565a1c8f-6fd9-4bab-9834-076eaea527f8@googlegroups.com> <7e1ed740-df18-4978-b11d-8ca9c4b5bd04@googlegroups.com> <46567a60-bb75-4f22-b968-bf3ccbf35afb@googlegroups.com> Date: Sat, 22 Jun 2013 11:37:41 +1000 Subject: Re: Default Value From: Chris Angelico To: python-list@python.org Content-Type: text/plain; charset=ISO-8859-1 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: 46 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1371865068 news.xs4all.nl 15886 [2001:888:2000:d::a6]:40380 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:48896 On Sat, Jun 22, 2013 at 11:15 AM, Rick Johnson wrote: > # Literal > py> d = {[1]:2} > Traceback (most recent call last): > File "", line 1, in > d = {[1]:2} > TypeError: unhashable type: 'list' > # Symbol > py> lst = [1] > py> d = {lst:2} > Traceback (most recent call last): > File "", line 1, in > d = {lst:2} > TypeError: unhashable type: 'list' > > Hmm, maybe only certain mutables work? Great, more esoteric > rules! Feel free to enlighten me since i'm not going to > waste one second of my time pursuing the docs just to learn > about ANOTHER unintuitive PyWart i have no use for. >>> class HashableList(list): def __hash__(self): return 42 >>> a=HashableList() >>> a [] >>> a.append(1) >>> a.append(2) >>> a.append(3) >>> a [1, 2, 3] >>> d={a:123} >>> d {[1, 2, 3]: 123} >>> a.append(4) >>> d[a] 123 It's nothing to do with mutability, all to do with hashability. And you can pick a number of different ways of hashing these objects, like going for the object's id(), or attempting to hash tuple(self) and falling back on id(), or anything you like. Easy. ChrisA