What F strings should have been
D'Arcy Cain
darcy at VybeNetworks.com
Wed Feb 14 16:24:23 EST 2018
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.
--
D'Arcy J.M. Cain
Vybe Networks Inc.
http://www.VybeNetworks.com/
IM:darcy at Vex.Net VoIP: sip:darcy at VybeNetworks.com
More information about the Python-list
mailing list