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 15:18:43 +0100 Lines: 45 Message-ID: References: <564dbe6b$0$1610$c3e8da3$5496439d@news.astraweb.com> <564df258$0$1604$c3e8da3$5496439d@news.astraweb.com> <564e71f6$0$1619$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 0HLNHIXnurYZvSuTqo7rdwiTcjB1KwymNzKzkTv1fdkQ== 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; 'else:': 0.03; 'static': 0.03; 'elif': 0.04; 'mutable': 0.09; 'parameter.': 0.09; 'slow.': 0.09; 'wrong,': 0.09; 'def': 0.13; 'argument': 0.15; 'behaviour.': 0.16; 'defaults,': 0.16; 'definition.': 0.16; 'received:adsl- dyn.isp.belgacom.be': 0.16; 'received:io': 0.16; 'received:psf.io': 0.16; 'two.': 0.16; 'who?': 0.16; 'worst': 0.16; 'wrote:': 0.16; '2015': 0.20; 'decorator': 0.22; 'am,': 0.23; 'header:In-Reply-To:1': 0.24; 'header:User-Agent:1': 0.26; 'example': 0.26; 'define': 0.27; 'fri,': 0.27; 'parameters': 0.27; 'defining': 0.27; 'function': 0.28; 'idea': 0.28; 'device': 0.28; 'once,': 0.29; 'received:192.168.1.3': 0.29; 'received:be': 0.30; 'another': 0.32; 'says': 0.32; 'third': 0.33; "d'aprano": 0.33; 'decorators': 0.33; 'steven': 0.33; 'nov': 0.35; 'should': 0.36; 'to:addr:python-list': 0.36; 'subject:?': 0.36; 'subject:: ': 0.37; 'really': 0.37; 'received:192': 0.39; 'to:addr:python.org': 0.40; 'where': 0.40; 'called': 0.40; 'easy': 0.60; 'your': 0.60; 'avoid': 0.61; 'body': 0.61; 'default': 0.61; 'charset:windows-1252': 0.62; "d'aprano:": 0.84; 'received:195.238': 0.84; 'schreef': 0.84; 'thirdly': 0.84; 'write:': 0.91 X-Belgacom-Dynamic: yes X-IronPort-Anti-Spam-Filtered: true X-IronPort-Anti-Spam-Result: A2CaAQC9cFRW/9Xi9VENUcYYgz2CUgKBdBEBAQEBAQEBhT8BAQR4EQsYCRYPCQMCAQIBRRMIAogvrS+MeYQmAQEBBwIBIIZUhH6ERGOEEgEEllCNMYFbln6DcjeEMYRTgUkBAQE User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:31.0) Gecko/20100101 Icedove/31.8.0 In-Reply-To: <564e71f6$0$1619$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:99339 Op 20-11-15 om 02:05 schreef Steven D'Aprano: > On Fri, 20 Nov 2015 04:30 am, BartC wrote: > >> On 19/11/2015 16:01, Steven D'Aprano wrote: > [...] > >> The whole concept of 'mutable' default is alien to me. A default is just >> a convenient device to avoid having to write: >> >> fn(0) or fn("") or fn([]) > > Says who? > > Here's another use for function defaults, as static storage: > > > # Ackermann's function > def ack(m, n, _memo={}): > key = m, n > if key not in _memo: > if m==0: v = n + 1 > elif n==0: v = ack(m-1, 1) > else: v = ack(m-1, ack(m, n-1)) > _memo[key] = v > return _memo[key] > > > This is a quick and easy way to memoise a function which would otherwise be > horribly slow. And it only works because _memo is bound to a mutable object > once, and once only. I think this is the worst kind of example you can give in defense for this kind of behaviour. Because you are defining a function as having three parameters where it really has only two. You really don't want this function to be called with that third parameter. Secondly, because we have decorators now, it really is not a good idea to define your function with the memoization in the body functionality. Thirdly your use with a default argument would go horribly wrong, should we try to use it in the decorator definition. -- Antoon.