Path: csiph.com!news.swapon.de!fu-berlin.de!uni-berlin.de!not-for-mail From: Antoon Pardon Newsgroups: comp.lang.python Subject: Re: What is a function parameter =[] for? Date: Tue, 24 Nov 2015 13:38:01 +0100 Lines: 57 Message-ID: References: <564dc478$0$1609$c3e8da3$5496439d@news.astraweb.com> Mime-Version: 1.0 Content-Type: text/plain; charset=windows-1252 Content-Transfer-Encoding: 7bit X-Trace: news.uni-berlin.de lBZ0qUA78MLqJvuj2AnHugjVfZoEvw/k5mFu89Hrbu3Q== Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.013 X-Spam-Evidence: '*H*': 0.97; '*S*': 0.00; 'that?': 0.05; 'executed': 0.07; 'only,': 0.07; 'variable,': 0.07; 'behave': 0.09; 'happen.': 0.09; 'ignoring': 0.09; 'situation.': 0.09; 'talks': 0.11; 'useful,': 0.13; 'argument': 0.15; 'result.': 0.15; 'thu,': 0.15; 'binding.': 0.16; 'received:adsl-dyn.isp.belgacom.be': 0.16; 'received:io': 0.16; 'received:psf.io': 0.16; 'segment': 0.16; 'surprising': 0.16; 'wrote:': 0.16; 'language': 0.19; '2015': 0.20; 'function:': 0.22; 'object.': 0.22; 'header:In-Reply-To:1': 0.24; 'header:User-Agent:1': 0.26; 'point.': 0.27; 'equivalent': 0.27; 'behaviour': 0.29; 'once,': 0.29; 'received:192.168.1.3': 0.29; 'tutorial': 0.29; 'print': 0.30; 'that.': 0.30; 'too.': 0.30; 'code': 0.30; 'received:be': 0.30; '[1]': 0.32; 'point': 0.33; 'problem': 0.33; 'doubt': 0.33; 'steven': 0.33; 'list': 0.34; 'could': 0.35; 'confirmed': 0.35; 'fresh': 0.35; 'nov': 0.35; "isn't": 0.35; 'sometimes': 0.35; 'but': 0.36; 'instead': 0.36; 'there': 0.36; 'possible': 0.36; 'to:addr:python-list': 0.36; 'subject:?': 0.36; 'pm,': 0.36; 'subject:: ': 0.37; 'really': 0.37; 'expect': 0.37; 'say': 0.37; 'version': 0.38; 'mean': 0.38; 'why': 0.39; 'received:192': 0.39; 'to:addr:python.org': 0.40; 'forget': 0.60; 'waiting': 0.60; 'skip:u 10': 0.61; 'body': 0.61; 'default': 0.61; 'real': 0.62; 'charset:windows-1252': 0.62; 'course': 0.62; 'more': 0.63; 'for:': 0.64; 'situation': 0.67; 'useful.': 0.72; '*simple*': 0.84; 'beside': 0.84; 'calls,': 0.84; "d'aprano:": 0.84; 'received:195.238': 0.84; 'schreef': 0.84 X-Belgacom-Dynamic: yes X-IronPort-Anti-Spam-Filtered: true X-IronPort-Anti-Spam-Result: A2CbAAANWVRW/9Xi9VENUcQhAQ2BZYYPAoFuFAEBAQEBAQGFPgEBAQMBeAYLCxgJFg8JAwIBAgFFEwgCF4gLDa1EjHyEJgEBCAIBIIZUhH6FJ4QSAQSWUI0xnEsfAQGER4YcAQEB User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:31.0) Gecko/20100101 Icedove/31.8.0 In-Reply-To: <564dc478$0$1609$c3e8da3$5496439d@news.astraweb.com> 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:99323 Op 19-11-15 om 13:45 schreef Steven D'Aprano: > On Thu, 19 Nov 2015 10:41 pm, BartC wrote: > > >> I expect the version with the default argument to be >> exactly the same as the last lot of calls, namely for: >> >> fn() >> fn() >> fn() >> >> to be equivalent to: >> >> temp=[] >> fn(temp) >> temp=[] >> fn(temp) >> temp=[] >> fn(temp) > > Why on earth would you expect that? > > I don't mean that it isn't sometimes useful. Of course it is sometimes > useful, there's no doubt about that. But why would you expect the language > to default to the *slow*, *expensive*, *complicated* behaviour instead of > the *fast*, *cheap*, *simple* behaviour? I would say because it is better understandable. The way default value behave now, is just a little less surprising as when the following segment of code would print: [1] a = [] a.append(1) b = [] print b Let us not forget that the tutorial talks about Default Argument *Values*. And also the language reference talks about precomputed *values*. What we really get is a precomputed object. > You already have one way to set the argument to a fresh list every time you > call the function: put the code you want executed inside the body of the > function. There's no need for a *second* way to get the same result. Which is beside the point. The point is understandability. > But if you want the default value to be evaluated exactly once, and once > only, there is no real alternative to early binding. You could use a global > variable, of course, but that is no solution -- that's a problem waiting to > happen. No more than the situation we have now. The situation we have now is a problem waiting to happen too. Which is confirmed again and again. You are stacking the deck by calling a possible alternative "a problem waiting to happen" while ignoring the problems that happen with the current situation.