Path: csiph.com!fu-berlin.de!uni-berlin.de!not-for-mail From: Chris Angelico Newsgroups: comp.lang.python Subject: Re: Late-binding of function defaults (was Re: What is a function parameter =[] for?) Date: Thu, 26 Nov 2015 22:04:06 +1100 Lines: 38 Message-ID: References: <87d1v5emhl.fsf@elektro.pacujo.net> <564e6a62$0$1620$c3e8da3$5496439d@news.astraweb.com> <56556DF8.1080407@rece.vub.ac.be> <878u5mns8z.fsf@elektro.pacujo.net> <874mganq2q.fsf@elektro.pacujo.net> <56565822$0$1617$c3e8da3$5496439d@news.astraweb.com> <87oaeh2qy0.fsf@elektro.pacujo.net> <87a8q1f9ni.fsf@elektro.pacujo.net> <87610pf2uq.fsf@elektro.pacujo.net> Mime-Version: 1.0 Content-Type: text/plain; charset=UTF-8 X-Trace: news.uni-berlin.de kBqfknSx9s19KK0L65PWdAclXuEsAfhbzGulwJGix5mw== Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.000 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'classes.': 0.07; 'defines': 0.07; 'keys,': 0.07; 'undefined': 0.07; 'cc:addr :python-list': 0.09; 'brackets': 0.09; 'dict': 0.09; 'mutable': 0.09; "object's": 0.09; 'tends': 0.09; 'type:': 0.09; 'typeerror:': 0.09; 'python': 0.10; 'assume': 0.11; 'python.': 0.11; 'exception': 0.13; 'subject: \n ': 0.15; 'thu,': 0.15; 'bucket': 0.16; 'fly.': 0.16; 'from:addr:rosuav': 0.16; 'from:name:chris angelico': 0.16; 'received:io': 0.16; 'received:psf.io': 0.16; 'subject:?)': 0.16; 'unhashable': 0.16; 'wrote:': 0.16; '>>>': 0.20; '2015': 0.20; 'cc:2**0': 0.20; 'cc:addr:python.org': 0.20; 'fix': 0.21; '"",': 0.22; 'enforce': 0.22; 'keys': 0.22; 'trying': 0.22; '(most': 0.24; 'header:In- Reply-To:1': 0.24; "doesn't": 0.26; 'chris': 0.26; 'figure': 0.27; 'define': 0.27; 'not.': 0.27; 'message-id:@mail.gmail.com': 0.27; 'fine': 0.28; 'behaviour': 0.29; 'dictionary': 0.29; 'hash': 0.29; 'occasionally': 0.29; 'programmers': 0.30; 'probably': 0.31; '[1]': 0.32; 'getting': 0.33; 'class': 0.33; 'problem': 0.33; 'usually': 0.33; 'traceback': 0.33; 'file': 0.34; 'lists': 0.34; 'received:google.com': 0.35; 'nov': 0.35; 'but': 0.36; 'should': 0.36; 'instead': 0.36; 'there': 0.36; 'received:209.85': 0.36; 'pm,': 0.36; 'subject:: ': 0.37; 'really': 0.37; 'being': 0.37; 'say': 0.37; 'received:209.85.213': 0.37; 'wanted': 0.37; 'received:209': 0.38; 'why': 0.39; 'sure': 0.39; 'enough': 0.39; 'subject:-': 0.39; 'where': 0.40; 'behavior': 0.61; 'skip:u 10': 0.61; 'is.': 0.63; '26,': 0.72; 'useful.': 0.72; 'intelligent': 0.76; 'square': 0.76; 'chrisa': 0.84; 'mistakes.': 0.84; 'pain': 0.84; 'to:none': 0.91 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=9IB/s040ZSANccbGtytE8gWFRpcC/G1E1qo/k3E/+9o=; b=bKLy6T86+D2RxW6/N46QMXXoJD47Nk4KrEQMI0X+JsaGN5/mOJrp7C2z4dZGfkbpcJ mKW8l/05iYuy7GaAfdJVCwRa4j6A35bdTAKqNH6YtXkW4GNLycosbZT/3ISKlRNdYklj gO2Qx98rtSJt/EMX4sNeQlwsVcxWg05f871WA73XQ+SvfuqOLe/mYp8kjpfwaV+l+9/9 p5+6Rx8qQFbOyIMtUo2Yb4sfa+DdIXvBkL4o8msgUI0AyFppp0gZ+i4GbVR/qN7Ub5Qc fRwIOP+tjNJpmwoCyveQA2nIeEj6+HiC6e2pSksZg29M4SjnctAKKwUJTJ7NA1850DCh xRKQ== X-Received: by 10.50.87.71 with SMTP id v7mr2436769igz.92.1448535846680; Thu, 26 Nov 2015 03:04:06 -0800 (PST) In-Reply-To: <87610pf2uq.fsf@elektro.pacujo.net> X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.20+ Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Xref: csiph.com comp.lang.python:99552 On Thu, Nov 26, 2015 at 9:54 PM, Marko Rauhamaa wrote: > Chris Angelico : > >> On Thu, Nov 26, 2015 at 7:27 PM, Marko Rauhamaa wrote: >>> I have wanted to use lists as keys, and there should be no reason to >>> allow mutable tuples. It should be enough to say that the behavior of >>> a dictionary is undefined if a key should mutate on the fly. >> >> Python defines dict-key-validity as being synonymous with hashability. >> It's up to the class author to make sure the object's hash is useful. > > >>> hash([]) > Traceback (most recent call last): > File "", line 1, in > TypeError: unhashable type: 'list' > > Annoying. Yes, it's really annoying that you get an immediate exception instead of unpredictably getting bizarre failures that depend on the exact bucket sizes and dict size and so on. It's such a pain to get told exactly where the problem is. >> Behaviour being undefined works just fine [1] in C. It's not so >> popular in Python. I don't think we want to have myriad new and >> experienced programmers trying to figure out why square brackets in >> dict keys are usually fine but occasionally not. > > But there's no way to enforce that with any of the myriad new classes. Python tends to assume that programmers are intelligent people who are prepared to fix their mistakes. As a general rule, if you define __eq__, you should probably define __hash__ as well, and then you need to know what you're doing; specifically, the invariant that if a==b, hash(a)==hash(b). Python doesn't enforce that because it's impossible to enforce. ChrisA