What F strings should have been
Ian Kelly
ian.g.kelly at gmail.com
Thu Feb 15 11:47:13 EST 2018
On Wed, Feb 14, 2018 at 2:24 PM, D'Arcy Cain <darcy at vybenetworks.com> 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]}")
> ...
> print(s(1, 2, dict(x=3)))
>
> Too bad that it is too late to assign f'' to that function.
Someday far in the future Python will be remembered as a language that
people in the before-time used to format strings.
More information about the Python-list
mailing list