Path: csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!aioe.org!feeder.news-service.com!newsfeed.xs4all.nl!newsfeed5.news.xs4all.nl!newsgate.cistron.nl!newsgate.news.xs4all.nl!194.109.133.85.MISMATCH!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; 'example:': 0.03; '2.x': 0.05; 'attributes': 0.05; 'int': 0.05; 'chunk': 0.07; 'formatting': 0.07; 'terry': 0.07; 'python': 0.08; '3.0,': 0.09; 'operator,': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:80.91.229.12': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'received:lo.gmane.org': 0.09; 'statement.': 0.09; 'subject:string': 0.09; 'tuple': 0.09; 'pm,': 0.10; '>>>': 0.12; 'def': 0.12; 'described': 0.14; 'wrote:': 0.14; 'finney': 0.16; 'iterator': 0.16; 'new-style': 0.16; 'reedy': 0.16; 'subject:formatting': 0.16; 'subject:syntax': 0.16; 'url:format': 0.16; 'wins': 0.16; '{0}': 0.16; 'subject:was': 0.16; 'jan': 0.20; 'header:In-Reply-To:1': 0.21; "haven't": 0.22; 'indicating': 0.23; 'objects': 0.23; 'code': 0.24; 'preferred': 0.26; 'missed': 0.26; 'string': 0.26; 'object': 0.26; 'testing': 0.27; 'example': 0.27; 'class': 0.29; 'bit': 0.30; 'chase': 0.30; 'style.': 0.30; 'tuples': 0.30; 'url:library': 0.31; 'header:X -Complaints-To:1': 0.32; 'does': 0.33; 'to:addr:python-list': 0.33; 'operations': 0.33; 'lines': 0.33; "i've": 0.33; '...': 0.34; 'source': 0.34; 'there': 0.35; 'header:User-Agent:1': 0.35; 'subject:What': 0.35; 'test': 0.35; 'subject:new': 0.37; 'members': 0.37; 'url:docs': 0.37; 'case': 0.37; 'url:python': 0.38; 'received:org': 0.38; 'url:org': 0.38; 'subject:: ': 0.38; 'some': 0.38; 'should': 0.39; 'unless': 0.39; 'abuse': 0.39; 'header:Mime-Version:1': 0.39; 'difficult': 0.39; 'to:addr:python.org': 0.39; 'feature': 0.40; 'more': 0.60; 'link': 0.64; 'here': 0.66; 'special': 0.66; 'became': 0.67; 'subject:this': 0.76; 'flexible,': 0.84; '\xe2\x80\x9cthis': 0.84 X-Injected-Via-Gmane: http://gmane.org/ To: python-list@python.org From: Terry Reedy Subject: Re: new string-formatting preferred? (was "What is this syntax ?") Date: Mon, 20 Jun 2011 22:17:07 -0400 References: <87pqm8qh7m.fsf@benfinney.id.au> <4DFFE9D5.30300@tim.thechases.com> Mime-Version: 1.0 Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: quoted-printable X-Gmane-NNTP-Posting-Host: rain.gmane.org User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.2.17) Gecko/20110414 Lightning/1.0b2 Thunderbird/3.1.10 In-Reply-To: <4DFFE9D5.30300@tim.thechases.com> 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: 48 NNTP-Posting-Host: 82.94.164.166 X-Trace: 1308622639 news.xs4all.nl 49184 [::ffff:82.94.164.166]:56169 X-Complaints-To: abuse@xs4all.nl Xref: x330-a1.tempe.blueboxinc.net comp.lang.python:8056 On 6/20/2011 8:46 PM, Tim Chase wrote: > On 06/20/2011 05:19 PM, Ben Finney wrote: >> =E2=80=9CThis method of string formatting is the new standard in >> Python 3.0, and should be preferred to the % formatting >> described in String Formatting Operations in new code.=E2=80=9D >> >> > > Is there a good link to a thread-archive on when/why/how .format(...) > became "preferred to the % formatting"? That is a controversial statement. > I haven't seen any great wins of > the new formatting over the classic style. Is there some great feature > of new-style formatting that I've missed out on that obviates bajillion= s > of lines of 2.x code? It does not abuse the '%' operator, it does not make a special case of=20 tuples (a source of bugs), and it is more flexible, especially=20 indicating objects to be printed. Here is a simple example from my code=20 that would be a bit more difficult with %. multi_warn =3D '''\ Warning: testing multiple {0}s against an iterator will only test the first {0} unless the iterator is reiterable; most are not.'''.format =2E.. print(multiwarn('function')) =2E.. print(multiwarn('iterator')) Here is a more complex example: class chunk(): def __init__(self, a, b): self.a,self.b =3D a,b c=3Dchunk(1, (3,'hi')) print('{0.__class__.__name__} object has attributes int a <{0.a}> and=20 tuple b with members <{0.b[0]}> and <{0.b[1]}>'.format(c)) >>> chunk object has attributes int a <1> and tuple b with members <3> and --=20 Terry Jan Reedy