Path: csiph.com!usenet.pasdenom.info!weretis.net!feeder4.news.weretis.net!ecngs!feeder2.ecngs.de!newsfeed.freenet.ag!news2.euro.net!newsgate.cistron.nl!newsgate.news.xs4all.nl!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; 'python,': 0.02; 'argument': 0.05; '21,': 0.07; 'defaults': 0.07; 'expressions': 0.07; 'great.': 0.07; 'arguments,': 0.09; 'emulate': 0.09; 'python': 0.11; 'def': 0.12; "%s'": 0.16; '11:19': 0.16; 'disallow': 0.16; 'languages)': 0.16; 'merely': 0.16; 'mutable': 0.16; 'semantics': 0.16; 'wrote:': 0.18; 'thu,': 0.19; 'programming': 0.22; 'sort': 0.25; 'values': 0.27; 'header:In- Reply-To:1': 0.27; 'idea': 0.28; 'function': 0.29; 'chris': 0.29; 'am,': 0.29; 'words': 0.29; 'nature': 0.30; 'message- id:@mail.gmail.com': 0.30; 'code': 0.31; 'changed.': 0.31; 'there.': 0.32; 'run': 0.32; 'fri,': 0.33; 'problem': 0.35; 'advice': 0.35; 'etc': 0.35; 'late': 0.35; 'one,': 0.35; 'but': 0.35; 'received:google.com': 0.35; 'everyone.': 0.36; 'skip:j 20': 0.36; 'should': 0.36; 'changing': 0.37; 'too': 0.37; 'to:addr :python-list': 0.38; 'functional': 0.39; 'to:addr:python.org': 0.39; 'most': 0.60; 'skip:o 30': 0.61; 'simply': 0.61; 'face': 0.64; 'more': 0.64; 'worth': 0.66; 'side': 0.67; 'dont': 0.67; '20,': 0.68; 'default': 0.69; 'obvious': 0.74; 'goal': 0.75; 'flies': 0.84; 'subject:Value': 0.84; 'tricky': 0.84; 'rusi': 0.91; '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:from:date:message-id:subject:to :content-type; bh=+pieMEunxJoJbAU/nFfMkCV2uLKKaZ5jWouy6j+O9G4=; b=gaUCwEIOfZ8HNpH1Kl/DX1HnOONQwPftLImZYeANfzFEYMQfgrEAc7gkmKb6ns7bfd f/uxDx0J1asiOFDYKXz0U7v3W8dDMJjhoTo/+187z9QQUGWQ5oL+0ZPciPQgo2fzHALD /x/oSxtlojNVij+ukLqYUlHlyQFdwt36pQxi5pMZuhDAJylBKL0e5IvdGCEzeuJ1tFb3 4MTB9J4DlSgduvbMlxS5wZR7Bk7l6+H/HZUyUQoU690McC0S+cjfu5DlquAzWCvDhrmF skvQ5KbC6VRPmGk43S1O07NaeWNXjpqwrQzTGPq05oWSdjlJduVCICd4DOBvCW/0iEyG m/AQ== X-Received: by 10.68.42.134 with SMTP id o6mr8612936pbl.149.1371751094160; Thu, 20 Jun 2013 10:58:14 -0700 (PDT) MIME-Version: 1.0 In-Reply-To: References: <7e6361d5-6619-4aaa-adda-8b5f01bde57f@googlegroups.com> <447dd1c6-1bb2-4276-a109-78d7a067b442@d8g2000pbe.googlegroups.com> <2e92b4c7-31be-40d2-a906-ab19f3630dfa@googlegroups.com> <09cfa648-ab87-4261-b8f0-bf493de00553@4g2000pbf.googlegroups.com> From: Ian Kelly Date: Thu, 20 Jun 2013 11:57:34 -0600 Subject: Re: Default Value To: Python 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: 35 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1371751097 news.xs4all.nl 15908 [2001:888:2000:d::a6]:38907 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:48816 On Thu, Jun 20, 2013 at 11:19 AM, Chris Angelico wrote: > On Fri, Jun 21, 2013 at 3:12 AM, rusi wrote: >> Python (and all the other 'cool' languages) dont have gotchas because >> someone malevolently put them there. >> In most cases, the problem is seen too late and the cost of changing >> entrenched code too great. >> Or the problem is clear, the solution is not etc etc. > > Or, in many MANY cases, the choice was the right one, but isn't > obvious to everyone. I think it's worth pointing out that changing function defaults to late-binding would merely change the nature of the gotcha, not eliminate it. words = ("one", "two", "red", "blue", "fish") def join_strings(strings=words): return ' '.join('%s %s' % (s, strings[-1]) for s in strings[:-1]) # Later: words = open("gettysburg_address.txt", "r").read().split() # Oops, the default argument of join_strings just changed. Additionally, with late-binding semantics the default values would no longer be default *values*. They would be initialization code instead, which sort of flies in the face of the idea that late-binding would somehow be better for functional programming -- if the default expressions have no side effects and since they don't depend on the function arguments, why should they need to run more than once? If the goal is indeed to make the the functions more functional, then the proper solution would be to keep the binding early but just disallow mutable defaults altogether -- which is tricky to achieve in Python, so we simply emulate it with the advice "don't use mutable function defaults".