3 ms·
I like f-strings but I don't like that there are at least five ways to format stings. 1. %-formatting [1] 2. str.format [2] 3. string.Template [3] 4. f-stri
by vova_hn2 2mo ago
I like f-strings but I don't like that there are at least five ways to format stings.
1. %-formatting [1]
2. str.format [2]
3. string.Template [3]
4. f-string [4]
5. t-string [5]
What happened to "one-- and preferably only one --obvious way to do it"? [6]
Also, the way string formatting interacts with logging is a total mess. People just pass f-strings to logging, which seems to be an intuitive way to do it. Except it limits your options if you want to collect structured logs and it doesn't allow you to use late evaluation based on log level.
[1] https://docs.python.org/3/library/string.html#format-examples https://docs.python.org/3/library/string.html#format-example...
[2] https://docs.python.org/3/library/stdtypes.html#str.format https://docs.python.org/3/library/stdtypes.html#str.format
[3] https://docs.python.org/3/library/string.html#string.Template https://docs.python.org/3/library/string.html#string.Templat...
[4] https://docs.python.org/3/reference/lexical_analysis.html#f-strings https://docs.python.org/3/reference/lexical_analysis.html#f-...
[5] https://docs.python.org/3/reference/lexical_analysis.html#t-strings https://docs.python.org/3/reference/lexical_analysis.html#t-...
[6] https://peps.python.org/pep-0020/ https://peps.python.org/pep-0020/
- gucci-on-fleek 2mo ago> What happened to "one-- and preferably only one --obvious way to do it"? There arguably still is—just use f-strings for everything, unless you need to support ancient Python, in which case use %-formatting. t-strings are a special case, but in theory most functions should only accept regular strings or templates, so there should only be one choice there too. > Also, the way string formatting interacts with logging is a total mess [...] it doesn't allow you to use late evaluation based on log level. This all seems to be a side-effect of the fact that string formatting produces static strings, so I don't think that there's much that can be done here (but maybe t-strings can be creatively used here somehow).
- deleted 2mo ago[deleted]
- vova_hn2 2mo ago> maybe t-strings can be creatively used here somehow Maybe they could, but I would be the first to oppose introducing creative ways to do logging or string formatting. Such basic things should be done in the most standard way possible to reduce cognitive effort required to read and understand the code. But the standard way is kinda ugly. Most people would expect f-strings to be used for "normal" string formatting and %-style to be used for logging, because it is the default and most codebases do it this way. Therefore, you basically forced to have (at least) two different formatting syntaxes in your codebase. I say "at least", because if the program serves html pages, you most likely also have some other template engine like jinja...
- deleted 2mo ago[deleted]
- JackSlateur 2mo agot-string main use case is SQL (for instance, in psycopg¹) You can write things like that without SQL injection: t"INSERT INTO mytable (first_name, last_name) VALUES ({first_name}, {last_name})" This is a very pleasant feature, very useful [1] https://www.psycopg.org/psycopg3/docs/basic/tstrings.html https://www.psycopg.org/psycopg3/docs/basic/tstrings.html