[Python-ideas] Simplifying .format() based string interpolation

Robert Hölzl robert.hoelzl at posteo.de
Fri Feb 7 00:41:11 CET 2014


Hello All,

I REALLY like str.format() from PEP3101 since it is simple to use, 
powerful and extendable.  Especially I like the possibility to use 
named fields for two reasons: readability AND it is less error prone. 

i.E. compare
"{name} is {value}  ({value:X} in hex)"
to
"{} is {} ({1:X} in hex)".


In a lot of cases all fields are identical with variables 
from the local/global name space. 
This means in the above example one had to write:

"{name} is {value}  ({value:X} in hex)".format(name=name, value=value)

But such statements are violating the DRY principles! 


How about introducing new string tokens beginning with 'f', 
which tells the parser to take over that job. 
In this case one simply had to write:

f"{name} is {value}  ({value:X} in hex)"

and the parser would replace this by

"{name} is {value}  ({value:X} in hex)".format( 
    **collections.ChainMap(locals(), globals()) )


Would be glad to hear your thoughts,
Robert



More information about the Python-ideas mailing list