What F strings should have been
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:da...@vex.net VoIP: sip:da...@vybenetworks.com -- https://mail.python.org/mailman/listinfo/python-list
Re: What F strings should have been
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"..."? -- https://mail.python.org/mailman/listinfo/python-list
Re: What F strings should have been
On 02/15/18 02:56, Peter Otten wrote: >> 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 Hmm. Hadn't thought of that. >> 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"..."? s = f"ABC {1} {0} {2[x]}" -- D'Arcy J.M. Cain Vybe Networks Inc. http://www.VybeNetworks.com/ IM:da...@vex.net VoIP: sip:da...@vybenetworks.com -- https://mail.python.org/mailman/listinfo/python-list
Re: What F strings should have been
On Wed, Feb 14, 2018 at 2:24 PM, 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]}") > ... > 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. -- https://mail.python.org/mailman/listinfo/python-list