Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #22451

Re: string interpolation for python

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 <lanyjie@yahoo.com>
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 <CABgq=FyhR+Ldujj3YKRBpVXRVoeoayuXZviUUcNx-gXnFiHLSw@mail.gmail.com> <CACoeR0xV=28j6rFHm1ep3gzH9C0sbDqyxRRwX-JrVm3G+rrXww@mail.gmail.com> <1333174946.18436.YahooMailNeo@web121506.mail.ne1.yahoo.com> <jl6ma0$ptg$1@dough.gmane.org> <mailman.1200.1333352587.3037.python-list@python.org> <4f7962b0$0$29981$c3e8da3$5496439d@news.astraweb.com>
Date Mon, 2 Apr 2012 02:11:46 -0700 (PDT)
From Yingjie Lan <lanyjie@yahoo.com>
Subject Re: string interpolation for python
To "python-list@python.org" <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 <lanyjie@yahoo.com>
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <http://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <http://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.1205.1333358032.3037.python-list@python.org> (permalink)
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

Show key headers only | View raw





----- Original Message -----
> From: Steven D'Aprano <steve+comp.lang.python@pearwood.info>
> To: python-list@python.org
> Cc: 
> Sent: Monday, April 2, 2012 4:26 PM
> Subject: Re: string interpolation for python
> 
> On Mon, 02 Apr 2012 00:39:42 -0700, Yingjie Lan wrote:
> 
>>>  You can already do essentially that without adding a special-case
>>>  string
>> 
>>>  formatting method to the general methods we already have.
>>> 
>>>>>>   balls = 5
>>>>>>   people = 3
>>>>>>   'The {people} people have {balls}
>>>  balls.'.format(**locals())
>>>  'The 3 people have 5 balls.'
>> 
>> 
>>  Clearly dynamic strings are much more powerful, allowing arbitrary
>>  expressions inside.
> 
> And so it may be a security risk, if user-input somehow ends up treated 
> as a dynamic string.
> 
> We already have three ways to evaluate arbitrary expressions:
> 
> * Python code
> * eval
> * exec
> 
> Why do we need yet another one?
> 
> 
>>  It is also more terse and readable, since we need no
>>  dictionary.
> 
> I think you mean terse and unreadable, since we need no dictionary. That 
> means that variables will be evaluated by magic from... where? Globals? 
> Local scope? Non-local scope? All of the above?
> 
> We already have one way of evaluating implicit variables using implicit 
> rules, namely regular Python code. Why do we need a second one?
> 
> 
>>  I would probably rather liken dynamic expressions as a little brother of
>>  computable documents in Mathematica. It is a new kind of expression,
>>  rather than formatting -- though it has formatting connections.
> 
> Why do we need a new kind of expression?
> 
> 
>>  Dynamic strings are mainly useful at time of writing readable code
>>  before compilation.
> 
> What does that mean?
> 
> 
>>  The compiler can choose to convert it into a string
>>  formatting expression, of course. To efficiently format strings at
>>  runtime, the best choice (especially
>>  for safty reasons) is string formatting, not evaluating a dynamic
>>  string.
> 
> So you're suggesting that we should have dynamic strings, but not 
> actually use dynamic strings. The compiler should just convert them to 
> regular string formatting.
> 
> Why not cut out the middle-man and just use regular string formatting?
> 


I believe non of the other three alternatives are as terse and readable.
We've got template based, formatting with dict, formatting with tuple.
They all require the coder extra effort:

Both template based and dict-based formatting require writing the
identifier three times:

>>> name = 'Peter'
>>> "Are you %(name)s"%{'name':name}
 
If dynamic string is used:
>>> "Are you $name$?"

Template:
>>> Template("Are you $name?").substitute(name=name)

It is three to one in compactness, what a magic 3!

Of course, the old C style way:

>>> "Are you %s?"%name

Almost as terse, but not as readable, especially
when there are many parts to substitute --
the coder and reader need to be careful 
to make sure the sequence is correct.

Why the Python community is so
hostile to new things now? 
Python has merits,
but it is far from being perfect.

Cheers,
Yingjie

Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

Re: string interpolation for python Yingjie Lan <lanyjie@yahoo.com> - 2012-04-02 00:39 -0700
  Re: string interpolation for python Jussi Piitulainen <jpiitula@ling.helsinki.fi> - 2012-04-02 11:01 +0300
  Re: string interpolation for python Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2012-04-02 08:26 +0000
    Re: string interpolation for python Chris Angelico <rosuav@gmail.com> - 2012-04-02 18:47 +1000
    Re: string interpolation for python Yingjie Lan <lanyjie@yahoo.com> - 2012-04-02 02:11 -0700
      Re: string interpolation for python Duncan Booth <duncan.booth@invalid.invalid> - 2012-04-02 10:19 +0000
      Re: string interpolation for python Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2012-04-02 11:54 +0000
        Re: string interpolation for python Yingjie Lan <lanyjie@yahoo.com> - 2012-04-02 05:40 -0700
          Re: string interpolation for python Laurent Claessens <moky.math@gmail.com> - 2012-04-02 15:02 +0200
            Re: string interpolation for python Yingjie Lan <lanyjie@yahoo.com> - 2012-04-02 07:25 -0700
            Re: Re: string interpolation for python Evan Driscoll <driscoll@cs.wisc.edu> - 2012-04-02 15:36 -0500
          Re: string interpolation for python mwilson@the-wire.com - 2012-04-02 10:46 -0400
            Re: string interpolation for python mwilson@the-wire.com - 2012-04-02 11:34 -0400
              Re: string interpolation for python Yingjie Lan <lanyjie@yahoo.com> - 2012-04-02 09:02 -0700
      Re: string interpolation for python rusi <rustompmody@gmail.com> - 2012-04-02 06:04 -0700
    Re: string interpolation for python Yingjie Lan <lanyjie@yahoo.com> - 2012-04-02 02:17 -0700
      Re: string interpolation for python alex23 <wuwei23@gmail.com> - 2012-04-02 22:47 -0700
    Re: string interpolation for python Chris Angelico <rosuav@gmail.com> - 2012-04-02 19:56 +1000
    Re: string interpolation for python Chris Rebert <clp2@rebertia.com> - 2012-04-02 03:23 -0700
    Re: string interpolation for python Yingjie Lan <lanyjie@yahoo.com> - 2012-04-02 04:46 -0700
    Re: string interpolation for python Yingjie Lan <lanyjie@yahoo.com> - 2012-04-02 05:00 -0700
    Re: string interpolation for python Chris Angelico <rosuav@gmail.com> - 2012-04-03 00:58 +1000
      Re: string interpolation for python Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2012-04-02 18:56 +0000
    Re: string interpolation for python Yingjie Lan <lanyjie@yahoo.com> - 2012-04-02 08:49 -0700
    Re: string interpolation for python Chris Angelico <rosuav@gmail.com> - 2012-04-03 08:38 +1000
      Re: string interpolation for python Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2012-04-02 23:18 +0000
    Re: string interpolation for python Yingjie Lan <lanyjie@yahoo.com> - 2012-04-02 18:57 -0700

csiph-web