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


Groups > comp.lang.python > #7092

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!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail
Return-Path <chris@rebertia.com>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.006
X-Spam-Evidence '*H*': 0.99; '*S*': 0.00; 'variable,': 0.07; 'arguments.': 0.09; 'skip:{ 20': 0.09; 'subject:string': 0.09; 'this:': 0.10; 'am,': 0.14; 'wrote:': 0.14; '9:15': 0.16; 'captured': 0.16; 'subject:formatting': 0.16; 'unpack': 0.16; 'cc:addr:python-list': 0.17; 'mon,': 0.17; 'idea?': 0.19; 'keyword': 0.19; 'mapping': 0.19; 'cheers,': 0.19; 'header:In- Reply-To:1': 0.21; 'cc:2**0': 0.22; "doesn't": 0.25; 'received:209.85.161': 0.26; 'string': 0.26; 'work.': 0.28; 'message-id:@mail.gmail.com': 0.28; 'cc:addr:python.org': 0.30; 'looks': 0.31; 'hi,': 0.31; 'print': 0.31; "isn't": 0.33; 'chris': 0.34; 'using': 0.35; 'subject:new': 0.37; 'received:google.com': 0.37; 'something': 0.37; 'received:209.85': 0.37; 'but': 0.38; 'subject:: ': 0.38; "i'd": 0.39; 'received:209': 0.39; 'i.e.': 0.39; 'subject:with': 0.39; 'han': 0.65; 'here': 0.66; 'solo': 0.68; 'sender:addr:chris': 0.84; 'subject:local': 0.84
DKIM-Signature v=1; a=rsa-sha256; c=relaxed/relaxed; d=rebertia.com; s=google; h=domainkey-signature:mime-version:sender:in-reply-to:references:date :x-google-sender-auth:message-id:subject:from:to:cc:content-type :content-transfer-encoding; bh=fn1A0RKMDMD+d6HGrphI69LJex+o2hvTGIzDF3eiJPo=; b=YjEt5UdAHFpCKCHm8H1PQt3PSUnfOMlOdEx81mWmtnPBy19LADiZlAbuyl6xB9x3jl jEj0HvNdSEk+o+6aokf91dUHM/KNbfzJ0lu7zyr97D3mb3jfmiYGlo/NczPU2mYNBrus QYKpYtgrX5058VR9H2dPnihURjkjHeZ6fwuNM=
DomainKey-Signature a=rsa-sha1; c=nofws; d=rebertia.com; s=google; h=mime-version:sender:in-reply-to:references:date :x-google-sender-auth:message-id:subject:from:to:cc:content-type :content-transfer-encoding; b=dl0ZE6bS3WV3UumFc8jnaUdoUGwJjQdOMyUwq9Cq7wp4stPj4SafU9nkBtT4kajoHM Oa42IRgtlsyvUJkJuyY3Q7G0gPXdKJ7U2UgHhC5OT0IdISXlfemzs80TtaweqViDYlE0 gRXKdKhrT5NtOST9ztPXAdS1cpVAedz9x7SBs=
MIME-Version 1.0
Sender chris@rebertia.com
In-Reply-To <BANLkTi=VUwFDv5otq=7CMngbWzHy3Fo50A@mail.gmail.com>
References <BANLkTi=VUwFDv5otq=7CMngbWzHy3Fo50A@mail.gmail.com>
Date Mon, 6 Jun 2011 09:21:14 -0700
X-Google-Sender-Auth CK3Ro3B_naCYu7h4zHVpihxQyXU
Subject Re: new string formatting with local variables
From Chris Rebert <clp2@rebertia.com>
To Jabba Laci <jabba.laci@gmail.com>
Content-Type text/plain; charset=UTF-8
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.2491.1307377282.9059.python-list@python.org> (permalink)
Lines 32
NNTP-Posting-Host 82.94.164.166
X-Trace 1307377282 news.xs4all.nl 49176 [::ffff:82.94.164.166]:40559
X-Complaints-To abuse@xs4all.nl
Xref x330-a1.tempe.blueboxinc.net comp.lang.python:7092

Show key headers only | View raw


On Mon, Jun 6, 2011 at 9: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?

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

You must use prefix-** in the call to unpack the mapping as keyword arguments.
Note that using locals() like this isn't best-practice.

Cheers,
Chris
--
http://rebertia.com

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


Thread

Re: new string formatting with local variables Chris Rebert <clp2@rebertia.com> - 2011-06-06 09:21 -0700
  Re: new string formatting with local variables Ben Finney <ben+python@benfinney.id.au> - 2011-06-07 10:11 +1000
    Re: new string formatting with local variables Ian Kelly <ian.g.kelly@gmail.com> - 2011-06-06 18:31 -0600
    Re: new string formatting with local variables Ian Kelly <ian.g.kelly@gmail.com> - 2011-06-06 18:38 -0600
    Re: new string formatting with local variables Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2011-06-07 03:57 +0000
      Re: new string formatting with local variables Ben Finney <ben+python@benfinney.id.au> - 2011-06-07 17:47 +1000

csiph-web