title: Are there downsides to using Python locals() for string formatting?
body: <p>This is not a duplicate, as this regards using the newer <code>str.format()</code>, <em>the above linked question is also of lower quality,</em> and I think this question is sufficiently different to justify its canonicity.</p>
  
  <p>The question:</p>
  
  <p>One might expect an error here, but we can provide the <code>str.format</code> method with unused keyword arguments.</p>
  
  <pre><code>&gt;&gt;&gt; '{a}{b}'.format(a='foo', b='bar', c='baz')
  'foobar'
  </code></pre>
  
  <p>This enables code like:</p>
  
  <pre><code>&gt;&gt;&gt; foo = 'bar'
  &gt;&gt;&gt; baz = 'fizzbuzz'
  &gt;&gt;&gt; '{foo}{baz}'.format(**locals())
  'barfizzbuzz'
  &gt;&gt;&gt; baz = datetime.datetime.now()
  &gt;&gt;&gt; '{foo}_{baz}'.format(**locals())
  'bar_2013-12-20 18:36:55.624000'
  </code></pre>
  
  <p><strong>Is this a good practice to do inside a closure like a function?</strong></p>
  
  <pre><code>def make_foo_time_string():
      foo = 'bar'
      baz = datetime.datetime.now()
      return '{foo}{baz}'.format(**locals())
  </code></pre>
  
  <p>What would be the possible downsides? Extra variables loaded using extra cycles? Is this a matter of convenience? <strong>I don't recall seeing this used much, would it be considered idiomatic of Python?</strong></p>
  
  <p><strong>Update</strong> I have found a canonical suggested usage using the old style string interpolation: <a href="https://wiki.python.org/moin/PythonSpeed/PerformanceTips" rel="nofollow">https://wiki.python.org/moin/PythonSpeed/PerformanceTips</a>
  Nevertheless, it does seem a rather old document.</p>
  
  <blockquote>
    <p>"Even better, for readability (this has nothing to do with efficiency other than yours as a programmer), use dictionary substitution:"</p>
  </blockquote>
  
  <pre><code>out = "&lt;html&gt;%(head)s%(prologue)s%(query)s%(tail)s&lt;/html&gt;" % locals()
  </code></pre>