Path: csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!news.chainon-marquant.org!news-transit.tcx.org.uk!rt.uk.eu.org!newsfeed.xs4all.nl!newsfeed5.news.xs4all.nl!xs4all!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.002 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'value,': 0.04; 'complaints,': 0.07; 'does.': 0.07; 'function,': 0.07; 'none:': 0.07; 'unexpected': 0.07; 'python': 0.08; 'function:': 0.09; 'global,': 0.09; 'mutable': 0.09; 'relies': 0.09; 'am,': 0.12; 'def': 0.13; 'stored': 0.13; 'argument': 0.15; 'subject:was': 0.15; 'binding,': 0.16; 'did?': 0.16; 'disadvantage': 0.16; 'expression)': 0.16; 'function?': 0.16; 'gotcha.': 0.16; 'misfeature': 0.16; 'subject:=': 0.16; 'subject:Early': 0.16; 'to:addr:pearwood.info': 0.16; 'to:addr:steve+comp.lang.python': 0.16; "to:name:steven d'aprano": 0.16; 'versa.': 0.16; '\xc2\xa0if': 0.16; 'cc:addr:python-list': 0.16; "wouldn't": 0.17; 'language': 0.17; 'wrote:': 0.18; '>>>': 0.18; 'arguments': 0.18; 'once,': 0.18; 'cc:no real name:2**0': 0.20; "haven't": 0.20; 'result.': 0.21; 'dec': 0.22; "doesn't": 0.22; 'header:In-Reply- To:1': 0.22; 'cc:2**0': 0.24; "python's": 0.24; 'creating': 0.25; 'code': 0.25; 'code.': 0.26; 'function': 0.27; 'url:mailman': 0.28; 'mine': 0.28; 'needed,': 0.28; 'message-id:@mail.gmail.com': 0.28; 'assuming': 0.29; 'cc:addr:python.org': 0.29; 'arguments.': 0.30; 'calculated': 0.30; 'confusion': 0.30; 'harm': 0.30; 'object.': 0.30; 'received:209.85.210.46': 0.30; 'received:mail- pz0-f46.google.com': 0.30; 'shifting': 0.30; '(the': 0.30; 'usually': 0.31; 'cases': 0.32; 'time:': 0.32; 'touch.': 0.32; 'break': 0.32; 'subject:]': 0.32; 'url:listinfo': 0.32; 'implement': 0.32; 'there': 0.33; 'fri,': 0.34; 'it.': 0.34; 'things': 0.34; 'another.': 0.34; 'deleted.': 0.34; 'elegant': 0.34; 'function.': 0.34; 'changing': 0.35; 'vice': 0.35; 'url:python': 0.36; '...': 0.36; 'two': 0.37; 'but': 0.37; 'received:google.com': 0.37; "there's": 0.37; 'think': 0.37; 'steven': 0.38; 'some': 0.38; 'received:209.85': 0.38; 'put': 0.38; 'fail': 0.39; 'url:org': 0.39; 'either': 0.39; 'should': 0.39; 'define': 0.39; 'received:209': 0.40; 'might': 0.40; 'one,': 0.40; 'more': 0.61; '2011': 0.61; 'body': 0.61; 'offers': 0.61; 'eliminate': 0.63; 'results': 0.63; 'cost': 0.63; 'harder': 0.64; 'vary': 0.64; 'strategy': 0.64; 'guarantee': 0.65; 'piece': 0.66; 'live,': 0.67; 'dealing': 0.69; 'evaluate': 0.71; 'offer': 0.72; 'why?': 0.77; 'advantages': 0.80; 'strategies': 0.82; 'binding.': 0.84; 'choices.': 0.84; 'expects': 0.84; 'namely': 0.84; 'together,': 0.84; 'variable:': 0.84; 'surprise': 0.97 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma; h=mime-version:in-reply-to:references:from:date:message-id:subject:to :cc:content-type:content-transfer-encoding; bh=4NqlMrnTJ3cOwKdEl9eVdSlWuf9wwl0nW5GIHfuxBX8=; b=jhRTkrUcDCG8+udqp+wAoFR/oo1v2mLGhklOosIEj2yuuX5DX7MdqXfxUADfoeNAPV 1pPLn8Vo4eljiXEG6LCfT00K1NkIDBjmnaWfdctlqDkCR8HlfzJBLEO13l+zRF3OrTM8 CPdQ5h4G9yjsR+rTdHul5s3z0kptj4vhByFdI= MIME-Version: 1.0 In-Reply-To: <4ef4a30d$0$29973$c3e8da3$5496439d@news.astraweb.com> References: <18f78d0d-1e70-4c7b-9033-1422e6edb6db@t13g2000yqg.googlegroups.com> <10c62dac-2750-4f08-8962-21952c1c0a0b@v31g2000prg.googlegroups.com> <5a7a7aab-a320-4429-a130-ffcfcf0ac174@v24g2000prn.googlegroups.com> <4EF440A9.7030101@stoneleaf.us> <9ljcutFemiU5@mid.individual.net> <9ljd41Fp3bU1@mid.individual.net> <4ef4a30d$0$29973$c3e8da3$5496439d@news.astraweb.com> From: Devin Jeanpierre Date: Fri, 23 Dec 2011 19:24:44 -0500 Subject: Re: Early and late binding [was Re: what does 'a=b=c=[]' do] To: "Steven D'Aprano" Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: quoted-printable Cc: python-list@python.org X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.12 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: 99 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1324686335 news.xs4all.nl 6861 [2001:888:2000:d::a6]:43079 X-Complaints-To: abuse@xs4all.nl Xref: x330-a1.tempe.blueboxinc.net comp.lang.python:17830 > To fake early binding when the language provides late binding, you still > use a sentinel value, but the initialization code creating the default > value is outside the body of the function, usually in a global variable: > > _DEFAULT_Y =3D [] # Private constant, don't touch. > > def func(x, y=3DNone): > if y is None: > y =3D _DEFAULT_Y > ... > > This separates parts of the code that should be together, and relies on a > global, with all the disadvantages that implies. No, you can just do def func(x, y=3D_DEFAULT_Y): ... -- Devin On Fri, Dec 23, 2011 at 10:49 AM, Steven D'Aprano wrote: > On Fri, 23 Dec 2011 13:13:38 +0000, Neil Cerutti wrote: > >> On 2011-12-23, Neil Cerutti wrote: >>> Is the misfeature that Python doesn't evaluate the default argument >>> expression every time you call the function? What would be the harm if >>> it did? >> >> ...you know, assuming it wouldn't break existing code. ;) > > It will. Python's default argument strategy has been in use for 20 years. > Some code will rely on it. I know mine does. > > There are two strategies for dealing with default arguments that I know > of: early binding and late binding. Python has early binding: the default > argument is evaluated once, when the function is created. Late binding > means the default argument is always re-evaluated each time it is needed. > > Both strategies are reasonable choices. Both have advantages and > disadvantages. Both have use-cases, and both lead to confusion when the > user expects one but gets the other. If you think changing from early to > late binding will completely eliminate the default argument "gotcha", you > haven't thought things through -- at best you might reduce the number of > complaints, but only at the cost of shifting them from one set of use- > cases to another. > > Early binding is simple to implement and simple to explain: when you > define a function, the default value is evaluated once, and the result > stored to be used whenever it is needed. The disadvantage is that it can > lead to unexpected results for mutable arguments. > > Late binding is also simple to explain, but a little harder to implement. > The function needs to store the default value as a piece of code (an > expression) which can be re-evaluated as often as needed, not an object. > > The disadvantage of late binding is that since the expression is live, it > needs to be calculated each time, even if it turns out to be the same > result. But there's no guarantee that it will return the same result each > time: consider a default value like x=3Dtime.time(), which will return a > different value each time it is called; or one like x=3Da+b, which will > vary if either a or b are changed. Or will fail altogether if either a or > b are deleted. This will surprise some people some of the time and lead > to demands that Python "fix" the "obviously buggy" default argument > gotcha. > > If a language only offers one, I maintain it should offer early binding > (the status quo). Why? Because it is more elegant to fake late binding in > an early binding language than vice versa. > > To fake late binding in a language with early binding, use a sentinel > value and put the default value inside the body of the function: > > =C2=A0 =C2=A0def func(x, y=3DNone): > =C2=A0 =C2=A0 =C2=A0 =C2=A0if y is None: > =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0y =3D [] > =C2=A0 =C2=A0 =C2=A0 =C2=A0... > > All the important parts of the function are in one place, namely inside > the function. > > To fake early binding when the language provides late binding, you still > use a sentinel value, but the initialization code creating the default > value is outside the body of the function, usually in a global variable: > > =C2=A0 =C2=A0_DEFAULT_Y =3D [] =C2=A0# Private constant, don't touch. > > =C2=A0 =C2=A0def func(x, y=3DNone): > =C2=A0 =C2=A0 =C2=A0 =C2=A0if y is None: > =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0y =3D _DEFAULT_Y > =C2=A0 =C2=A0 =C2=A0 =C2=A0... > > This separates parts of the code that should be together, and relies on a > global, with all the disadvantages that implies. > > > > -- > Steven > -- > http://mail.python.org/mailman/listinfo/python-list