What F strings should have been

Peter Otten __peter__ at web.de
Thu Feb 15 03:56:07 EST 2018


D'Arcy Cain wrote:

> A recent post by Terry Jan Reedy got me thinking about formatting.  I
> like the new(ish) format method for strings and I see some value in F
> strings but it only works well with locals.  Anything more starts
> getting messier than format() and it is supposed to be cleaner.  Also, I
> find that I tend to re-use format strings and wish there was a
> formatting string option.  Here is what I came up with.
> 
> class FSTR(str):
>   def __call__(self, *args):
>     return self.format(*args)
> 
> And here is how it could be used.
> 
> s = FSTR("ABC {1} {0} {2[x]}")

This can be simplified to

s = "ABC {1} {0} {2[x]}".format

> ...
> print(s(1, 2, dict(x=3)))
> 
> Too bad that it is too late to assign f'' to that function.

I must be missing something. How would you simplify or improve the 
readability of the above with your version of f"..."? 





More information about the Python-list mailing list