4 ms·
I don't know about performant. If the difference is significant, that can be something to take into account, depending on the situation. Otherwise readability i
by roelschroeven 6y ago
I don't know about performant. If the difference is significant, that can be something to take into account, depending on the situation. Otherwise readability is more important.
So we come to readability, and I have to say that to me .format() is more readable. Honestly.
Regarding runtime errors, I presume you mean .format() doesn't prevent mismatches between the number of placeholders and the number of arguments, as in '{} {}'.format(42)? That's indeed a point in favor of f'', that I hadn't previously considered.
- mixmastamyk 6y agoThe eff string uses a special instruction that makes it not need to do expensive function lookups at runtime. These are costly in Python. Better in loops. Readability is a win in my book. You mention too much is happening in your example string. Ok, perhaps it is. But remember, just because the interpolation supports expressions, doesn't mean you have to put them in there. I'd argue your function call would be better done on the line above, with only the result put in the string. This is the most readable version so far I think: c = math.gcd(a, b) f'gcd({a}, {b}) = {c}' Syntax highlighting helps out as well. Yes about placehoders. Also that in some cases of format() each variable is repeated a number of times: 'hello {name}'.format(name=name) When this is changed it is a bug magnet. DRY in a nutshell. Rarely is a feature a home run like this one. Not to mention I was using it in batch files, shells, and perl twenty-five years ago.