new string formatting with local variables

Ben Finney ben+python at benfinney.id.au
Mon Jun 6 20:11:01 EDT 2011


Chris Rebert <clp2 at rebertia.com> writes:

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

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.

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

Who says so, and do you find their argument convincing? Do you have a
reference for that so we can see why?

-- 
 \       “If you write the word ‘monkey’ a million times, do you start |
  `\                      to think you're Shakespeare?” —Steven Wright |
_o__)                                                                  |
Ben Finney



More information about the Python-list mailing list