Path: csiph.com!usenet.pasdenom.info!gegeweb.org!de-l.enfer-du-nord.net!feeder2.enfer-du-nord.net!newsfeed.eweka.nl!eweka.nl!feeder3.eweka.nl!newsfeed.xs4all.nl!newsfeed6.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!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; 'python,': 0.02; 'string.': 0.04; 'syntax': 0.04; 'function,': 0.07; 'parsing': 0.07; 'python': 0.09; '>>>>': 0.09; 'formatting': 0.09; 'python:': 0.09; 'rules.': 0.09; 'subject:string': 0.09; 'mon,': 0.11; 'subject:python': 0.11; 'ignore': 0.13; '>>>': 0.14; 'cases': 0.14; 'module': 0.15; 'benefit.': 0.16; 'fiddle': 0.16; 'formatting,': 0.16; 'from:addr:rosuav': 0.16; 'from:name:chris angelico': 0.16; 'function:': 0.16; 'impossible': 0.16; 'magic': 0.16; 'yingjie': 0.16; 'cheap': 0.17; 'this:': 0.17; '(or': 0.18; 'string': 0.19; 'wrote:': 0.19; 'written': 0.21; 'versions': 0.21; 'differ': 0.21; 'explicit': 0.21; 'mechanism': 0.21; 'simpler': 0.21; 'header:In-Reply-To:1': 0.22; 'demonstrate': 0.23; 'implemented': 0.23; 'raw': 0.23; 'code.': 0.23; 'fairly': 0.23; 'somewhere': 0.23; 'that.': 0.24; 'pass': 0.24; 'message- id:@mail.gmail.com': 0.24; 'realize': 0.26; 'ignored.': 0.27; 'propose': 0.27; 'common': 0.28; 'work,': 0.28; 'idea,': 0.28; 'strings,': 0.28; 'subject:skip:i 10': 0.28; 'worked': 0.29; "i'm": 0.29; 'evaluation': 0.30; 'older': 0.30; 'received:209.85.210.174': 0.30; 'received:mail- iy0-f174.google.com': 0.30; 'chris': 0.31; 'received:209.85': 0.32; 'received:google.com': 0.32; "aren't": 0.32; 'like:': 0.32; 'code': 0.32; 'could': 0.32; 'received:209.85.210': 0.33; 'pm,': 0.34; 'something': 0.34; 'break': 0.34; 'mainly': 0.34; 'called': 0.34; 'two': 0.35; 'to:addr:python-list': 0.35; 'received:209': 0.35; 'there': 0.36; 'another': 0.36; 'easily': 0.36; 'copying': 0.37; 'but': 0.37; 'far': 0.37; 'dynamic': 0.37; 'subject:: ': 0.37; "didn't": 0.38; 'under': 0.38; 'to:addr:python.org': 0.39; 'header:Received:5': 0.40; 'skip:" 10': 0.40; 'real': 0.61; '2012': 0.62; 'worth': 0.62; 'kind': 0.63; 'here': 0.64; 'special': 0.74; 'clearer': 0.84; 'distinguish': 0.84; 'hassle.': 0.84; 'hood': 0.84; "it'd": 0.84; 'notion': 0.84; 'pasting': 0.84; 'zen': 0.84; 'proposal,': 0.91 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:date:message-id:subject:from:to :content-type:content-transfer-encoding; bh=gw6almRX0TPnx4evFr6JM6vv+q7qWAoYJ2Ksj3aGeKo=; b=YV4iBglgXcUPOdbiHC8/cvBRR1v/avmGtk584RfJlrvEcRQsivf8coaXz4ddmqsTgg MLsErKYgvNMYEiRc1O1X+DYrQaMzM9bl0Gsa+q1/KbvfcUPkQyVKYTgELVe7jWUgpOpk iUqdXuyds/o6+128FE/ap3eS1sr5ifS71gNpYrnn/QfMFz7bUfNX33U8KOL7LMhXuwdE hr/rCY2mO2HaUjACvtVEQuuFOKTcjbeAOubDHbheEVQI1dO7LRI7HM9eB0Sh99nmK4NZ fsY15kj3zQ0BB8m8OEldDVkFxg0MgtAWov3xNCB/jpfw/67HfV6QqwLNIBIKaQLsNL3J V5bQ== MIME-Version: 1.0 In-Reply-To: <1333351461.27538.YahooMailNeo@web121506.mail.ne1.yahoo.com> References: <1333174946.18436.YahooMailNeo@web121506.mail.ne1.yahoo.com> <1333351461.27538.YahooMailNeo@web121506.mail.ne1.yahoo.com> Date: Mon, 2 Apr 2012 17:52:33 +1000 Subject: Re: string interpolation for python From: Chris Angelico To: python-list@python.org 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 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: 60 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1333353157 news.xs4all.nl 6846 [2001:888:2000:d::a6]:50919 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:22445 On Mon, Apr 2, 2012 at 5:24 PM, Yingjie Lan wrote: > They are called "Dynamic strings". > Dynamic strings can achieve formatting, > but the mechanism under the hood differ > from common strings dramatically. > > Many here didn't realize that this is not > another formatting proposal, it is a new > kind of *expression*. > > To have it in Python, we will need > a new kind of syntax to distinguish it > from other strings, such as=A0raw strings > and the like. A raw string=A0looks like: > >>>> r'my\\ raw str' > 'my\\\\ raw str' > > A dynamic string may look like this: > >>>> name =3D "Peter" #common string >>>> d"Thank you, $name$!" #dynamic string! > 'Thank you, Peter!' >From the Zen of Python: Special cases aren't special enough to break the rules. It's fairly cheap to propose a new function. If people don't like it, they can ignore it... it can get stuck into a module somewhere and be easily ignored. If people DO like it, they can back-port it to older versions by simply copying and pasting the Python code (or "equivalent Python code", if the main version's written in C). New syntax like this has to be implemented in the parser, is (practically) impossible to back-port with pure Python, and has far higher potential to break existing code. The onus is on you to demonstrate that this syntax is worth this hassle. I'm -1 on the idea, mainly because there are already two perfectly good string interpolation syntaxes that don't require a new kind of string. I'm also against the notion of "interpolated strings" vs "non-interpolated strings" in general, having worked with them in PHP and not found any real benefit. But if you want it, you could fairly easily do something with an explicit interpolation parsing function: >>> d"Thank you, $name$!" #dynamic string! 'Thank you, Peter!' becomes: >>> d("Thank you, $name$!") #dynamic string! 'Thank you, Peter!' To make the magic of evaluation scope work, though, you'd need to fiddle around with tracebacks. It'd be far simpler and clearer to pass locals() and/or globals() to your function, if indeed you want that. Chris Angelico