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 00:35:38 +1100 Lines: 66 Message-ID: References: <87d1v5emhl.fsf@elektro.pacujo.net> <564e6a62$0$1620$c3e8da3$5496439d@news.astraweb.com> <56556DF8.1080407@rece.vub.ac.be> Mime-Version: 1.0 Content-Type: text/plain; charset=UTF-8 X-Trace: news.uni-berlin.de zdbSonsCTarKwgtOCzx97QrMtkF58Q02PolEC8nocKTA== Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.004 X-Spam-Evidence: '*H*': 0.99; '*S*': 0.00; 'python,': 0.02; 'cc:addr :python-list': 0.09; 'assigning': 0.09; 'semantics': 0.09; 'wed,': 0.15; 'subject: \n ': 0.15; 'conforms': 0.16; 'different?': 0.16; 'exactly?': 0.16; 'fits': 0.16; 'from:addr:rosuav': 0.16; 'from:name:chris angelico': 0.16; 'happened?': 0.16; 'received:io': 0.16; 'received:psf.io': 0.16; 'somewhere.': 0.16; 'subject:?)': 0.16; 'wrote:': 0.16; 'later': 0.16; 'looked': 0.16; 'runs': 0.18; 'language': 0.19; '2015': 0.20; 'cc:2**0': 0.20; 'cc:addr:python.org': 0.20; 'affected': 0.22; 'arrays': 0.22; 'constant': 0.22; 'object.': 0.22; 'pass': 0.22; 'seems': 0.23; 'references': 0.23; 'previously': 0.24; 'header:In-Reply-To:1': 0.24; 'example': 0.26; 'least': 0.27; 'question': 0.27; 'message- id:@mail.gmail.com': 0.27; 'function': 0.28; 'another.': 0.29; 'array': 0.29; "i'm": 0.30; "i'd": 0.31; '[1]': 0.32; 'date.': 0.33; 'foo': 0.33; 'reference,': 0.33; "i'll": 0.33; 'similar': 0.33; 'changing': 0.34; 'definition': 0.34; 'except': 0.34; 'received:google.com': 0.35; 'nov': 0.35; 'something': 0.35; "isn't": 0.35; 'but': 0.36; 'received:209.85': 0.36; 'pm,': 0.36; 'subject:: ': 0.37; 'expect': 0.37; 'received:209.85.213': 0.37; 'received:209': 0.38; 'names': 0.38; 'someone': 0.38; 'copying': 0.38; 'represent': 0.38; 'why': 0.39; 'sure': 0.39; 'does': 0.39; 'easily': 0.39; 'subject:-': 0.39; 'your': 0.60; "you'll": 0.61; 'more': 0.63; 'yourself,': 0.72; 'apart.': 0.84; 'chrisa': 0.84; 'to:none': 0.91; 'different.': 0.91; 'write:': 0.91; 'good,': 0.93; 'magical': 0.93 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=aejjCDe4MoAgSemLAPhCtbcs1+brN5DPjVyPVnQ7RGA=; b=oERre3rhXPvf66tSd4YzEXDiHpxQnEsFf6XA9qxGCihsrTuIBOZ0MrPUNpLlI7hsP1 UKmb1RlP/sb8/HwA0rqoo+Wdbo7xuIWj5Rz5dJaHt7mQDZzPTD1bd7wuHtuJ+4mYRyeD 77m2NW338k333Zl6nMDbHMkD41TQ+vJISvYg6xYsHQCrShlh2gOffABWfPa4M22MRyJ6 vfpskhRFMol4XQqqlYQ1beviPZt7zkDatOM0FpYlf5Yh3nQrSSi7R9ZBvC4dxgYp32/I qPnJrYXwALBgtcGk2AHdA8yjVKB8SrLrftcabY7PX5nV8n1JjRAe0eMMjFPbR64bwS0D qzDQ== X-Received: by 10.50.83.38 with SMTP id n6mr4179208igy.92.1448458538733; Wed, 25 Nov 2015 05:35:38 -0800 (PST) In-Reply-To: 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:99448 On Wed, Nov 25, 2015 at 11:35 PM, BartC wrote: > One gotcha at least is well known. Where, in a language of the least > surprises, you might write: > > d1 = date (25,12,2010) > d2 = d1 > d2.year += 1 > > You would expect d1 and d2 to represent a period one year apart. In Python, > they would both be the later date. I'm not sure why this is "a language of least surprises". It's just a language of *different* surprises. I'll give you an example of exactly that: PHP has semantics similar to what you're describing. $ php foo [1] => bar ) I'm not exactly sure of the definition of PHP's assignment, as it's been a while since I looked it up; but it seems to be something like what you're describing. This seems well and good, until you start contemplating gigantic arrays and what happens when you pass them from one function to another. What're your performance expectations? With Python, I know that "a = b" happens in constant time, and I also know that the performance of "a[1]=2" is not going to be affected by any other names that might be bound to the same object. PHP's references make this even more messy: $ php foo [1] => baz ) So... since I'd previously set $b to be a reference, now assigning to it does something different. And what, exactly? Is $a[1] the exact same array as $a? Has any copying happened? When I change $b[1], is that the same as changing $a[1], or is it actually changing $a[1][1], and is that even any different? This isn't a magical solution to all surprises, except perhaps in that you've built a language yourself, so it conforms perfectly to *your* expectations. You can never make something that fits everyone's expectations, so you'll always have surprises somewhere. It's just a question of where, and how easily they can be explained when someone runs into them. ChrisA