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

Robert Hölzl robert.hoelzl at posteo.de
Fri Feb 7 02:24:32 CET 2014


Chris Angelico <rosuav at ...> writes:

> On Fri, Feb 7, 2014 at 10:41 AM, Robert Hölzl 
<robert.hoelzl <at> posteo.de> wrote:
> > 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()) )
> 
> PHP programmers would love it. Subsequent maintainers would hate it.

In my Optinion the bad thing with PHP is that you can create fields that 
contain complex expressions (even whole programs). But the nice thing
 of .format are its restrictions: it allows only getattr() and getitem() 
operations...

> 
> It's way too magical for my liking.

mmh; why magical?
 - it is explicit (the string is marked by 'f')
 - it is side effect free
 - does not change the behaviour of an existing feature



More information about the Python-ideas mailing list