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


Groups > comp.lang.python > #7107

Re: new string formatting with local variables

Path csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!aioe.org!feeder.news-service.com!newsfeed.xs4all.nl!newsfeed6.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail
Return-Path <ericsnowcurrently@gmail.com>
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; 'args': 0.07; 'variable,': 0.07; 'skip:{ 20': 0.09; 'subject:string': 0.09; 'url:dev': 0.09; 'this:': 0.10; 'am,': 0.14; 'wrote:': 0.14; '[1].': 0.16; 'captured': 0.16; 'close:': 0.16; 'explanation': 0.16; 'subject:formatting': 0.16; 'unpacking': 0.16; 'argument': 0.16; 'cc:addr:python-list': 0.17; 'mon,': 0.17; 'call.': 0.19; 'idea?': 0.19; 'keyword': 0.19; 'thanks,': 0.19; 'header:In-Reply-To:1': 0.21; 'cc:2**0': 0.22; "doesn't": 0.25; 'url:mailman': 0.26; 'string': 0.26; 'work.': 0.28; 'message-id:@mail.gmail.com': 0.28; 'cc:addr:python.org': 0.30; 'url:listinfo': 0.30; 'received:209.85.215': 0.30; 'received:209.85.215.46': 0.30; 'received:mail-ew0-f46.google.com': 0.30; 'looks': 0.31; 'hi,': 0.31; 'print': 0.31; '[1]': 0.34; 'subject:new': 0.37; 'received:google.com': 0.37; 'something': 0.37; 'received:209.85': 0.37; 'url:docs': 0.37; 'url:python': 0.38; 'url:org': 0.38; 'but': 0.38; 'subject:: ': 0.38; "i'd": 0.39; 'received:209': 0.39; 'i.e.': 0.39; 'subject:with': 0.39; 'format': 0.40; 'han': 0.65; 'here': 0.66; 'solo': 0.68; 'subject:local': 0.84
DKIM-Signature v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma; h=domainkey-signature:mime-version:in-reply-to:references:date :message-id:subject:from:to:cc:content-type :content-transfer-encoding; bh=mpZ5biFg93+Y9Eo73Dz6PtLSqY6NASuN4LBQyM+UT4A=; b=tvSw2iwYJHM1qX5hXW7uyaqqiDImunXM/6kl1hUz/ugDDRCNgI+Z/AR42F9jt2yuuo /FccX8CcyheWeyXST7JQV11QUJ8aKA3i1rjg7UpQrBExIa4LAnZy05jGRL0yYiZCNvYZ wheH8kRBEf8DitZoTjGFN/ts+t34+8aMa92uA=
DomainKey-Signature a=rsa-sha1; c=nofws; d=gmail.com; s=gamma; h=mime-version:in-reply-to:references:date:message-id:subject:from:to :cc:content-type:content-transfer-encoding; b=EG0QRzSQtgurL3ySgU7/N9ZqaBGxG95voSrXHLClMdXdOLaU50S7LVAoPQQi7ca6N/ 19lCHo4PxTq9K65Y5Uwl0KiknNI87ILK5DXxdurtSkOIqDtCWJQ0MAOOJqG9J2pxCs85 oheLgokvCVox//xZ1owpubSY9k4xCwAra6U4Q=
MIME-Version 1.0
In-Reply-To <BANLkTi=VUwFDv5otq=7CMngbWzHy3Fo50A@mail.gmail.com>
References <BANLkTi=VUwFDv5otq=7CMngbWzHy3Fo50A@mail.gmail.com>
Date Mon, 6 Jun 2011 14:22:49 -0600
Subject Re: new string formatting with local variables
From Eric Snow <ericsnowcurrently@gmail.com>
To Jabba Laci <jabba.laci@gmail.com>
Content-Type text/plain; charset=ISO-8859-1
Content-Transfer-Encoding quoted-printable
Cc Python mailing list <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 <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.2504.1307391772.9059.python-list@python.org> (permalink)
Lines 42
NNTP-Posting-Host 82.94.164.166
X-Trace 1307391772 news.xs4all.nl 49180 [::ffff:82.94.164.166]:33161
X-Complaints-To abuse@xs4all.nl
Xref x330-a1.tempe.blueboxinc.net comp.lang.python:7107

Show key headers only | View raw


On Mon, Jun 6, 2011 at 10:15 AM, Jabba Laci <jabba.laci@gmail.com> wrote:
> Hi,
>
> I'd like to simplify the following string formatting:
>
> solo = 'Han Solo'
> jabba = 'Jabba the Hutt'
> print "{solo} was captured by {jabba}".format(solo=solo, jabba=jabba)
> # Han Solo was captured by Jabba the Hutt
>
> What I don't like here is this: "solo=solo, jabba=jabba", i.e. the
> same thing is repeated. In "solo=solo", the left part is a key and the
> right part is the value of a local variable, but it looks strange.
>
> I'd like something like this:
> print "{solo} was captured by {jabba}".format(locals())        # WRONG!
>
> But it doesn't work.
>
> Do you have any idea?
>

You were close:

print "{solo} was captured by {jabba}".format(**locals())

This will turn locals() into keyword args for the format call.  The
tutorial has a good explanation on argument unpacking [1].

-eric

[1] http://docs.python.org/dev/tutorial/controlflow.html#unpacking-argument-lists


> Thanks,
>
> Laszlo
> --
> http://mail.python.org/mailman/listinfo/python-list
>

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


Thread

Re: new string formatting with local variables Eric Snow <ericsnowcurrently@gmail.com> - 2011-06-06 14:22 -0600

csiph-web