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: 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: References: 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 To: Jabba Laci Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: quoted-printable Cc: Python mailing list 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: 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 On Mon, Jun 6, 2011 at 9:15 AM, Jabba Laci wrote: > Hi, > > I'd like to simplify the following string formatting: > > solo =3D 'Han Solo' > jabba =3D 'Jabba the Hutt' > print "{solo} was captured by {jabba}".format(solo=3Dsolo, jabba=3Djabba) > # Han Solo was captured by Jabba the Hutt > > What I don't like here is this: "solo=3Dsolo, jabba=3Djabba", i.e. the > same thing is repeated. In "solo=3Dsolo", 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()) =C2=A0 =C2=A0 =C2= =A0 =C2=A0# 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 argumen= ts. Note that using locals() like this isn't best-practice. Cheers, Chris -- http://rebertia.com