5 ms·
There should be one-- and preferably only one --obvious way to do it. This would be the 4th way of formatting strings in Python.
by Walkman 11y ago
There should be one-- and preferably only one --obvious way to do it.
This would be the 4th way of formatting strings in Python.
- JoshTriplett 11y agoWhich clearly implies that the first three were insufficiently obvious. Or insufficiently Dutch.
- chrismorgan 11y agoIt would also render the previous ways obsolete. New-style formatting never took over completely from old-style because it wasn’t sufficiently compelling—`"%s %s" % (a, b)` versus `"{} {}".format(a, b)` doesn’t have a clear winner. But `f"{a} {b}"`? Clearly superior. With the exception of backwards compatibility matters (which will be a nuisance for far too long), there would really be no reason to keep using the old ways in most places. i18n/l10n would really be the only mainstream reason for using anything other than f-strings.
- voyou 11y ago"It would also render the previous ways obsolete." I don't think it would. If I understand this PEP correctly, the "format" method is significantly more dynamic. For instance, I don't think this new PEP would allow for cases where the template isn't stored directly in the program, or where the values to interpolate are not local variables. So you would still need to keep str.format around for those use cases.
- wylee 11y agoYour example isn't very compelling, but for longer strings with more complex formatting, I'd say .format() is pretty compelling. I find that in general '{x} blah blah blah {y}'.format(x=x, y=y) is more readable than '%s blah blah blah %s' % (x, y) even if the former is a bit longer. On top of that, there's a whole bunch of stuff you can do with .format() that just isn't possible with %.
- cnvogel 11y agoEven .format() is quite redundant for the "standard" usage, as we had string interpolation using '%' with dicts for a long time. '%s blah blah blah %s' % (x, y) '{x} blah blah blah {y}'.format(x=x, y=y) '%(x)s blah blah blah %(y)s'%{'y':y, 'x':x}
- baq 11y agoit'd be the first one that's safe* in runtime. *obligatory "not really, but in most cases" disclaimer
- ceronman 11y agoAlthough practicality beats purity.