I like them for the most part. Almost always more readable to out things in the brackets rather than the .format at the end, and always better than the %. Occasionally I still use .format with really long strings though for readability.
thought that might result in lots of other edits to the source, this in particular seems unwarranted "foo".encode("utf-8") --> "foo".encode() because the default encoding in py3+ is utf-8. What happened to explicit is better than implicit?
Yeah, I’d say not all rules in pyupgrade are universally popular. Fortunately I seldom see it used as a CI checker or pre-commit hook, and much more often used as a one-off transition tool with output manually reviewed instead (more like 2to3 than black).
AFAIK the % operator is not deprecated. In fact % formatting being syntactically near identical to C is a feature to me. I use format strings as well depending on context.
% formatting being older doesn't mean it's obsolete.
That said it is cool to design a script to transform your code and any time you spent in the developing of automation is time earned.
It’s not a proof, but it makes logical sense: f-strings can be noted and compiled at runtime, whereas regular strings must be compiled every time string.format notices them. Maybe middling on one run, but reasonably useful in a long loop
I worked with code generators that use extensively `.format` or `%`. The code of those generators is too difficult to follow. In my previous job I was able to rewrite from scratch over of those generators with saner jinja2 templates. But for inherited/external code that task is not always viable. So, f-strings bring the benefit of being clearer, while keeping the existing code.
It is only my personal opinion but I find the f-strings to be conceptually bad:
It might be a little bit easier to writer, but now you have magic and also code that is mixed within what should be simple strings. It is so easy to have suprising bad side effects because some operation would have been hidden in strings.
I liked the ability to put semi-complex anonymous expressions in format and reference them with {}. If you did this it was still be obvious what was going on without having to create variable names like comma_separated_list_of_emails.
Technically this is possible in f strings but it leads to an escaping mess. You wouldn't want to put a comprehension with a ','.join in there, for example.
Even when you did use variable names (e.g. "{addr_list}...".format(addr_list=...") you could safely use shorter names because the scope was tighter.
So easy? I’ve never created a side effect in my life with f-strings.
The same could be said about virtually any feature of any language.
Why would you want to impose artificial constraints on yourself? I really dislike the argument “people will hurt themselves, let’s go back to the old way“
Having a choice on when to use the existing string formatting methods is a feature. I tend to use ‘%’ a lot for it’s compactness and f-strings most more rarely (admittedly there doesn’t seem to be much reason to use .format these days).
In general I don’t understand the need to hard-standardize code. Making subtle decisions depending on context is an important part of programming, please let humans decide and don’t use these types of tools in commit hooks...
WRT .format, '%' is safer to use with untrusted (well, semi-trusted) format strings, whereas .format can lead to code injection (in more insidious ways than with f-strings).
BTW, this is a great resource that compares the ways to do different formatting actions in two different styles - the %-based one (what they call "old style") and the .format one ("new style"): https://pyformat.info/
41 comments
[ 4.8 ms ] story [ 54.4 ms ] thread[1]: https://github.com/asottile/pyupgrade
I guess you need the `--py36-plus` CLI option to reproduce the desired behaviour https://github.com/asottile/pyupgrade#f-strings
thought that might result in lots of other edits to the source, this in particular seems unwarranted "foo".encode("utf-8") --> "foo".encode() because the default encoding in py3+ is utf-8. What happened to explicit is better than implicit?
AFAIK the % operator is not deprecated. In fact % formatting being syntactically near identical to C is a feature to me. I use format strings as well depending on context.
% formatting being older doesn't mean it's obsolete.
That said it is cool to design a script to transform your code and any time you spent in the developing of automation is time earned.
I can see an in-house style guide strongly recommending f-strings over % formatting
Hmm. I have a long habit of (almost) always using a one-element tuple. Consider:
The % operator on strings has a special case for tuples: If you really want str(x) and are unsure that x might be a tuple, then you must use the tuple form: or for a more involved example: This might occur with input validation, like: In my code base there are at least 347 occurrences of this form:It might be a little bit easier to writer, but now you have magic and also code that is mixed within what should be simple strings. It is so easy to have suprising bad side effects because some operation would have been hidden in strings.
r"" strings for raw representation (this breaks most syntax highlighters though)
u"" for unicode string
b"" for bytes string.
f"" would totally fit as a "formatted string".
(R&B strings were available in python2, for context).
Personally I find the use case between simple %-interpolation of dumb strings and full blown templates pretty small.
I liked the ability to put semi-complex anonymous expressions in format and reference them with {}. If you did this it was still be obvious what was going on without having to create variable names like comma_separated_list_of_emails.
Technically this is possible in f strings but it leads to an escaping mess. You wouldn't want to put a comprehension with a ','.join in there, for example.
Even when you did use variable names (e.g. "{addr_list}...".format(addr_list=...") you could safely use shorter names because the scope was tighter.
The same could be said about virtually any feature of any language.
Why would you want to impose artificial constraints on yourself? I really dislike the argument “people will hurt themselves, let’s go back to the old way“
In general I don’t understand the need to hard-standardize code. Making subtle decisions depending on context is an important part of programming, please let humans decide and don’t use these types of tools in commit hooks...
Yes, and so is deciding what context matters across a whole team or team-of-teams.
Whenever I see this word, I see a trust or communication problem.
It was a fun experience :)