Path: csiph.com!usenet.pasdenom.info!weretis.net!feeder1.news.weretis.net!feeder.erje.net!newsfeed.xs4all.nl!newsfeed6.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.000 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'string.': 0.04; 'essentially': 0.05; 'one?': 0.05; '(especially': 0.07; 'compiler': 0.07; 'correct.': 0.07; 'exec': 0.07; 'expressions': 0.07; 'skip:% 20': 0.07; 'strings.': 0.07; 'python': 0.09; 'alternatives': 0.09; 'formatting': 0.09; 'identifier': 0.09; 'skip:$ 30': 0.09; 'subject:string': 0.09; 'tuple.': 0.09; 'mon,': 0.11; 'template': 0.11; 'subject:python': 0.11; '>>>': 0.14; 'to:name:python-list@python.org': 0.14; '"are': 0.16; 'above?': 0.16; 'balls': 0.16; 'dictionary.': 0.16; 'eval': 0.16; 'evaluating': 0.16; 'expression,': 0.16; 'formatting,': 0.16; 'formatting.': 0.16; 'from...': 0.16; 'from:addr:lanyjie': 0.16; 'from:name:yingjie lan': 0.16; 'magic': 0.16; 'perfect.': 0.16; 'readable': 0.16; 'received:124.205': 0.16; 'reply- to:addr:lanyjie': 0.16; 'reply-to:name:yingjie lan': 0.16; 'risk,': 0.16; 'substitute': 0.16; 'suggesting': 0.16; 'used:': 0.16; 'way:': 0.16; 'where?': 0.16; 'yingjie': 0.16; 'variables': 0.17; 'string': 0.19; 'wrote:': 0.19; 'implicit': 0.21; 'header :In-Reply-To:1': 0.22; 'code.': 0.23; 'second': 0.25; 'cheers,': 0.26; 'non': 0.26; 'monday,': 0.27; 'sequence': 0.27; 'regular': 0.27; '-0700,': 0.28; "d'aprano": 0.28; 'efficiently': 0.28; 'received:98.138.87': 0.28; 'steven': 0.28; 'strings,': 0.28; 'subject:skip:i 10': 0.28; 'convert': 0.28; 'reply- to:addr:yahoo.com': 0.28; 'especially': 0.28; 'skip:b 20': 0.30; 'probably': 0.32; 'code': 0.32; 'method': 0.33; 'rather': 0.33; 'ends': 0.34; 'mainly': 0.34; 'mean': 0.34; 'to:addr:python-list': 0.35; 'things': 0.35; 'useful': 0.35; 'should': 0.35; '-----': 0.35; 'methods': 0.35; 'there': 0.36; 'does': 0.36; 'another': 0.36; 'being': 0.36; 'email addr:python.org': 0.37; 'but': 0.37; 'community': 0.37; 'far': 0.37; 'subject:': 0.37; 'why': 0.37; 'dynamic': 0.37; 'subject:: ': 0.37; 'from:': 0.38; 'means': 0.38; 'old': 0.38; 'to:addr:python.org': 0.39; 'sure': 0.40; 'email name :python-list': 0.62; 'evaluate': 0.62; '2012': 0.62; 'kind': 0.63; 'more': 0.63; 're:': 0.64; 'choose': 0.64; 'cut': 0.65; 'header :Reply-To:1': 0.67; '4:26': 0.84; 'based,': 0.84; 'dict,': 0.84; 'hostile': 0.84; 'namely': 0.84; "'the": 0.91; 'have.': 0.91 X-Yahoo-Newman-Property: ymail-3 X-Yahoo-Newman-Id: 203920.55007.bm@omp1007.mail.ne1.yahoo.com DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=yahoo.com; s=s1024; t=1333357907; bh=Mp/MyyXZ5ZOYNLlQoNr91JpXJo9oMOaBmuakCsmQpR8=; h=X-YMail-OSG:Received:X-Mailer:References:Message-ID:Date:From:Reply-To:Subject:To:In-Reply-To:MIME-Version:Content-Type:Content-Transfer-Encoding; b=jkGyZecDPLloFiSLu2Dd3rbq3ZwnGyqDeqx6/6rKz4NdmhBLHbQ+hSP+n8qZZeXMEf7InNRE03eil4zzkvHvGGrtCFpERRPEO9z7rXNXusVYj8S4ZDyv0EawQyI4tt8cYK2dgRqib7EYh1MVzWOFGEpyWpEfmEb9D3eF3OabHtA= DomainKey-Signature: a=rsa-sha1; q=dns; c=nofws; s=s1024; d=yahoo.com; h=X-YMail-OSG:Received:X-Mailer:References:Message-ID:Date:From:Reply-To:Subject:To:In-Reply-To:MIME-Version:Content-Type:Content-Transfer-Encoding; b=wtOdr3VkjnSqCf6ohkqOt0Fy+3hfsWj2ILbMO02xpAOAASpM6CClGPQQnF0aOH9aBkPhLvyByHERiWgWsJiyqCxfKYGuBkJa1zgPdHaAk/i6EyDQapqbwfEIyWcOjODoqqYBkb2BsTon75207nMRsHQOUeQPmhGj58subRR8gu8=; X-YMail-OSG: .O__zbYVM1nKRb4Wc7LR4baqTeRVH2StRiEJ5JYnruoFK5g u5UKDgQipejoLmvtlh_VjJMB.sxDae_AZhadji6yVWURKhM3tqlqHxrPHXQU u3r7LdlcX17TodkaQssvG0EdQQpRcN7VkfSnyKlGBq1Fy7zE_mdeJ3e3w17K Zi_z9VdpTvE7CEH2sIhy7HPYp2G7lUjlmYN_tY6YT7Ra8pTTie.4nH85tMM. rA2Et2DQbSjnM6r4dkilcoxaEpmwjNJwUpJk5dvKltK35VannD5WbL9HfS9z .swgveTDWITHlI8K23YRX.4L2EWJblVgDGoy5DQEfjKWcobrgnFAoOLbAmYW JrmwDc4XeK4IjeR.KXaWl3RxB12J0lFDS.kvzUQzteeYTrwaVEe_WKqMhxdB amear4XL6VtWmw4C9YUqgUBLE7Fp_q.f4d3FTFKa.nKl7p8KFltDdhrYT9BL WwEXooQARR099N7faaY6lQUQ- X-Mailer: YahooMailWebService/0.8.117.340979 References: <1333174946.18436.YahooMailNeo@web121506.mail.ne1.yahoo.com> <4f7962b0$0$29981$c3e8da3$5496439d@news.astraweb.com> Date: Mon, 2 Apr 2012 02:11:46 -0700 (PDT) From: Yingjie Lan Subject: Re: string interpolation for python To: "python-list@python.org" In-Reply-To: <4f7962b0$0$29981$c3e8da3$5496439d@news.astraweb.com> MIME-Version: 1.0 Content-Type: text/plain; charset=iso-8859-1 Content-Transfer-Encoding: quoted-printable X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.12 Precedence: list Reply-To: Yingjie Lan 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: 46 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1333358032 news.xs4all.nl 6909 [2001:888:2000:d::a6]:43857 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:22451 =0A=0A=0A=0A----- Original Message -----=0A> From: Steven D'Aprano =0A> To: python-list@python.org=0A> Cc: =0A> = Sent: Monday, April 2, 2012 4:26 PM=0A> Subject: Re: string interpolation f= or python=0A> =0A> On Mon, 02 Apr 2012 00:39:42 -0700, Yingjie Lan wrote:= =0A> =0A>>> You can already do essentially that without adding a special-c= ase=0A>>> string=0A>> =0A>>> formatting method to the general methods we = already have.=0A>>> =0A>>>>>> =A0 balls =3D 5=0A>>>>>> =A0 people =3D 3=0A>= >>>>> =A0 'The {people} people have {balls}=0A>>> balls.'.format(**locals(= ))=0A>>> 'The 3 people have 5 balls.'=0A>> =0A>> =0A>> Clearly dynamic st= rings are much more powerful, allowing arbitrary=0A>> expressions inside.= =0A> =0A> And so it may be a security risk, if user-input somehow ends up t= reated =0A> as a dynamic string.=0A> =0A> We already have three ways to eva= luate arbitrary expressions:=0A> =0A> * Python code=0A> * eval=0A> * exec= =0A> =0A> Why do we need yet another one?=0A> =0A> =0A>> It is also more t= erse and readable, since we need no=0A>> dictionary.=0A> =0A> I think you = mean terse and unreadable, since we need no dictionary. That =0A> means tha= t variables will be evaluated by magic from... where? Globals? =0A> Local s= cope? Non-local scope? All of the above?=0A> =0A> We already have one way o= f evaluating implicit variables using implicit =0A> rules, namely regular P= ython code. Why do we need a second one?=0A> =0A> =0A>> I would probably r= ather liken dynamic expressions as a little brother of=0A>> computable doc= uments=A0in Mathematica. It is a new kind of expression,=0A>> rather than = formatting -- though it has formatting connections.=0A> =0A> Why do we need= a new kind of expression?=0A> =0A> =0A>> Dynamic strings are mainly usefu= l at time of writing readable code=0A>> before compilation.=0A> =0A> What = does that mean?=0A> =0A> =0A>> The compiler can choose to convert it into = a string=0A>> formatting expression, of course. To efficiently format=A0st= rings at=0A>> runtime, the best choice (especially=0A>> for safty reasons= ) is string formatting, not evaluating a dynamic=0A>> string.=0A> =0A> So = you're suggesting that we should have dynamic strings, but not =0A> actuall= y use dynamic strings. The compiler should just convert them to =0A> regula= r string formatting.=0A> =0A> Why not cut out the middle-man and just use r= egular string formatting?=0A> =0A=0A=0AI believe non of the other three alt= ernatives are as terse and readable.=0AWe've got template based, formatting= with dict, formatting with tuple.=0AThey all require the coder extra effor= t:=0A=0ABoth template based and dict-based formatting require writing the= =0Aidentifier three times:=0A=0A>>> name =3D 'Peter'=0A>>> "Are you %(name)= s"%{'name':name}=0A=A0=0AIf dynamic string is used:=0A>>> "Are you $name$?"= =0A=0ATemplate:=0A>>> Template("Are you $name?").substitute(name=3Dname)=0A= =0AIt is three to one in compactness, what a magic 3!=0A=0AOf course, the o= ld C style way:=0A=0A>>> "Are you %s?"%name=0A=0AAlmost as terse, but not a= s readable, especially=0Awhen there are many=A0parts to substitute --=0Athe= coder and reader need to be careful=A0=0Ato=A0make sure the sequence is co= rrect.=0A=0AWhy the Python community is so=0Ahostile to new things now?=A0= =0APython has merits,=0Abut it is far from being perfect.=0A=0ACheers,=0AYi= ngjie