[Python-Dev] PEP-498: Literal String Formatting

Eric V. Smith eric at trueblade.com
Mon Aug 10 19:18:16 CEST 2015


On 08/10/2015 01:07 PM, Steven D'Aprano wrote:
> On Sun, Aug 09, 2015 at 06:14:18PM -0700, David Mertz wrote:
> 
> [...]
>> That said, there *is* one small corner where I believe f-strings add
>> something helpful to the language.  There is no really concise way to spell:
>>
>>   collections.ChainMap(locals(), globals(), __builtins__.__dict__).
> 
> I think that to match the normal name resolution rules, nonlocals() 
> needs to slip in there between locals() and globals(). I realise that 
> there actually isn't a nonlocals() function (perhaps there should be?). 
> 
>> If we could spell that as, say `lgb()`, that would let str.format() or
>> %-formatting pick up the full "what's in scope".  To my mind, that's the
>> only good thing about the f-string idea.
> 
> I like the concept, but not the name. Initialisms tend to be hard 
> to remember and rarely self-explanatory. How about scope()?

I don't see how you're going to be able to do this in the general case.
Not all variables end up in locals(). See PEP-498's discussion of
closures, for example. Guido has already said locals() and globals()
would not be part of the solution for string interpolation (also in the
PEP).

PEP-498 handles the non-general case: it parses through the string to
find the variables used in the expressions, and then adds them to the
symbol table.

Eric.




More information about the Python-Dev mailing list