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


Groups > comp.lang.python > #7142

Re: new string formatting with local variables

Path csiph.com!x330-a1.tempe.blueboxinc.net!newsfeed.hal-mli.net!feeder1.hal-mli.net!border3.nntp.dca.giganews.com!border1.nntp.dca.giganews.com!nntp.giganews.com!postnews.google.com!news1.google.com!news.glorb.com!news.astraweb.com!border6.newsrouter.astraweb.com!not-for-mail
From Ben Finney <ben+python@benfinney.id.au>
Newsgroups comp.lang.python
Subject Re: new string formatting with local variables
References <BANLkTi=VUwFDv5otq=7CMngbWzHy3Fo50A@mail.gmail.com> <mailman.2491.1307377282.9059.python-list@python.org> <87zklu1ox6.fsf@benfinney.id.au> <4deda1c6$0$29980$c3e8da3$5496439d@news.astraweb.com>
X-Public-Key-ID 0xAC128405
X-Public-Key-Fingerprint 517C F14B B2F3 98B0 CB35 4855 B8B2 4C06 AC12 8405
X-Public-Key-URL http://www.benfinney.id.au/contact/bfinney-pubkey.asc
X-Post-From Ben Finney <bignose+hates-spam@benfinney.id.au>
Date Tue, 07 Jun 2011 17:47:58 +1000
Message-ID <871uz613rl.fsf@benfinney.id.au> (permalink)
User-Agent Gnus/5.13 (Gnus v5.13) Emacs/23.3 (gnu/linux)
Cancel-Lock sha1:+kgkPFPMbhjNW8OKxx/5X9sbsRo=
MIME-Version 1.0
Content-Type text/plain; charset=utf-8
Content-Transfer-Encoding 8bit
Lines 34
Organization Unlimited download news at news.astraweb.com
NNTP-Posting-Host 2b95e3c0.news.astraweb.com
X-Trace DXC=TT7UfeY^<ciJ?XMfTm0i_cL?0kYOcDh@jSBc;\8ijUdk:F=AP1hJGNmUGD@iN[Hd?o=RcCTXA3hIf
Xref x330-a1.tempe.blueboxinc.net comp.lang.python:7142

Show key headers only | View raw


Steven D'Aprano <steve+comp.lang.python@pearwood.info> writes:

> On Tue, 07 Jun 2011 10:11:01 +1000, Ben Finney wrote:
>
> > I tend to use ‘u"foo {bar} baz".format(**vars())’, since ‘vars’ can
> > also take the namespace of an object. I only need to remember one
> > “give me the namespace” function for formatting.
[…]

>
> It's a code smell. Why is this code messing with locals() instead of 
> using names explicitly? Is it possible that this code will attempt to 
> modify locals()? I need to look twice to be sure its safe. Why do you 
> need to pass *all* of the locals if only two names are used?

The names are explicit; they're in the format string. It's because I
don't want to repeat the names several times that I'm making use of the
dictionary provided by ‘vars()’.

> Where possible, I'd rather be explicit about which names I want:
>
> solo = "Han Solo"
> jabba = "Jabba the Hutt"
>
> "{hero} was captured by {villain}.".format(hero=solo, villain=jabba)

See, repeating those name references makes *me* hinky. I used those
names because they refer to things I've already named.

-- 
 \        “The difference between religions and cults is determined by |
  `\                      how much real estate is owned.” —Frank Zappa |
_o__)                                                                  |
Ben Finney

Back to comp.lang.python | Previous | NextPrevious 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