Path: csiph.com!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 14:43:33 +0100 Lines: 49 Message-ID: References: <564dbe6b$0$1610$c3e8da3$5496439d@news.astraweb.com> <564df258$0$1604$c3e8da3$5496439d@news.astraweb.com> <8601c9af-a7d9-4642-ba1c-8edd1e4c3390@googlegroups.com> Mime-Version: 1.0 Content-Type: text/plain; charset=windows-1252 Content-Transfer-Encoding: 7bit X-Trace: news.uni-berlin.de 79A4FWTI9N4E47X183kNVA1Oi8LNVFrq6hKGPiPqv7jg== Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.003 X-Spam-Evidence: '*H*': 0.99; '*S*': 0.00; 'value,': 0.03; '(python': 0.05; 'dynamically': 0.07; 'argument,': 0.09; 'constants.': 0.09; 'mutable': 0.09; 'specified,': 0.09; 'python': 0.10; 'thursday,': 0.13; 'argument': 0.15; 'value.': 0.15; '42;': 0.16; 'evaluated.': 0.16; 'received:adsl-dyn.isp.belgacom.be': 0.16; 'received:io': 0.16; 'received:psf.io': 0.16; 'scopes': 0.16; 'wrote:': 0.16; 'tells': 0.18; 'python?': 0.18; 'language': 0.19; '>>>': 0.20; 'changes': 0.20; '2015': 0.20; 'constant': 0.22; 'object.': 0.22; 'produces': 0.22; 'python"': 0.22; 'consistent': 0.23; 'examples': 0.24; 'header:In-Reply-To:1': 0.24; 'header:User-Agent:1': 0.26; 'question': 0.27; 'values': 0.28; 'arguments,': 0.29; 'received:192.168.1.3': 0.29; 'work.': 0.30; 'received:be': 0.30; "can't": 0.32; 'language.': 0.32; 'topic': 0.32; 'problem': 0.33; 'common': 0.33; 'michael': 0.33; 'surprised': 0.33; 'values.': 0.33; 'languages': 0.34; 'lists': 0.34; 'but': 0.36; 'list,': 0.36; 'alone': 0.36; 'to:addr:python-list': 0.36; 'subject:?': 0.36; 'pm,': 0.36; 'subject:: ': 0.37; 'being': 0.37; 'expect': 0.37; 'method': 0.37; 'missing': 0.37; 'late': 0.38; 'names': 0.38; 'mean': 0.38; 'means': 0.39; 'does': 0.39; "didn't": 0.39; 'received:192': 0.39; 'to:addr:python.org': 0.40; 'your': 0.60; 'default': 0.61; 'charset:windows-1252': 0.62; 'more': 0.63; 'different': 0.63; 'times': 0.63; 'binding': 0.66; 'here': 0.66; 'surprise': 0.72; 'received:195.238': 0.84; 'schreef': 0.84; 'involved.': 0.91 X-Belgacom-Dynamic: yes X-IronPort-Anti-Spam-Filtered: true X-IronPort-Anti-Spam-Result: A2CaAQAzZlRW/9Xi9VENUcQygWWDPYJSAoFzEgEBAQEBAQGFPwEBBHgRCxgJFg8JAwIBAgFFEwgCiC+tTYx5hCYBAQEHAgEghlSEfoUnhBIBBJZQjTGBW5Z+g3IoA4Q9hhwBAQE User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:31.0) Gecko/20100101 Icedove/31.8.0 In-Reply-To: <8601c9af-a7d9-4642-ba1c-8edd1e4c3390@googlegroups.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:99329 Op 20-11-15 om 01:27 schreef Ned Batchelder: > On Thursday, November 19, 2015 at 7:11:52 PM UTC-5, BartC wrote: >> On 19/11/2015 22:55, Michael Torrie wrote: >>> On 11/19/2015 02:21 PM, BartC wrote: >>>> (Python returns 42; so that means my languages are more dynamic than >>>> Python? That's hard to believe!) >>> >>> It tells me your language does late binding for default arguments, which >>> does mean the default argument can dynamically change at call time, >>> which would surprise me if I didn't know about it. Either form of >>> binding is acceptable, and I don't think it makes a language more or >>> less dynamic. >> >> You get the expression that is specified, which can give different >> values at different times unless it involves only constants. >> >> It can't be exactly the same as writing an identical expression in place >> of the missing argument, as apparently different scopes come into play >> if names are involved. >> >> However I mainly use them for constant values. And [] is a constant >> value in my opinion. >> >> -- >> Bartc > > You are not alone in being surprised by how mutable default values work. > It is a topic that comes up in every "What's Bad About Python" discussion, > and is a common question from new users of the language. > > I can understand how you would view [] as a constant value. It's true that > it is an expression that produces a consistent value each time it is > evaluated. But that value is a list, and lists are mutable. The examples > here all use the .append() method on that value, which changes it. I think that part of the problem is, that [] is not a constant object. So that when you see a line like ls = [] It behaves more lke ls = [].copy() than what you would expect with the normal python semantics. -- Antoon