[Python-ideas] Briefer string format

Eric V. Smith eric at trueblade.com
Mon Jul 20 19:08:37 CEST 2015

On 07/20/2015 10:19 AM, Eric V. Smith wrote:
> On 07/20/2015 10:08 AM, Ryan Gonzalez wrote:
>> I would prefer something more like:
>> def f(s):
>> caller = inspect.stack()[1][0]
>> return s.format(dict(caller.f_globals, **caller.f_locals))
> You need to use format_map (or **dict(...)). And ChainMap might be a
> better choice, it would take some benchmarking to know.
> Also, you don't get builtins using this approach. I'm using eval to
> exactly match what evaluating the variable in the parent context would
> give you. That might not matter depending on the actual requirements.
> But I agree there are multiple ways to do this, and several of them
> could be made to work. Mine might have fatal flaws that more testing
> would show.

My quick testing comes up with this, largely based on the code by joejev:

import sys
import collections

def f(str):
    frame = sys._getframe(1)
    return str.format_map(collections.ChainMap(

I'm not sure about the builtins, but this seems to work. Also, you might
want to be able to pass in the frame depth to allow this to be callable
more than 1 level deep.

So, given that this is all basically possible to implement today (at the
cost of using sys._getframe()), I'm -1 on adding any compiler tricks to
support this via syntax. From what I know of PyPy, this should be
supported there, albeit at a large performance cost.


More information about the Python-ideas mailing list