Comments are especially useful in multiline f-strings. There is no problem to insert them as part of the string, if it is supported downstream, like re module in verbose mode. But in other cases this can be either not possible or not readable (e.g no syntax highlighting).
Right now there is no documented way to add comments to f-strings. Python docs keep silence, PEP 498 says this is not possible.
(Added later) It is possible to comment on f-string expression, but not on a line of text:
f'''
foo = {
1 + 2 # comment
}
...
text that needs a comment { # syntax error
}
'''
f'{'comment':.0}'
f'{'comment'[:0]}'
f'{'comment' and ''}'
f'{'comment' * 0}'
But there is no preferred or recommended way to do it. Having a documented solution will provide some benefits, for example, allow syntax highlighting in IDE plugins.
Possible solutions
Keeping aside introducing completely new syntax,
f'text {.comment}'
f'text {//comment}'
f'text {--comment}'
f'text {?comment}'
f'text {?:comment}' # similar to re
these could be:
Document one of “syntax hacks” above — this will allow plugin developers to implement syntax highlighting. Optimizations may be added to not produce excessive bytecode in this case.
Add new conversion !i or !x that will ignore the whole expression:
Why are they any more useful in multiline f-strings than in normal multiline strings?
If the expressions you’re interprolating are complex, extract those into standalone variables calculated before the f-string, and comment the calculation just like any normal Python expression (which it is).
The difference is that for normal multiline strings it is really impossible, and for f-strings there is a formatting syntax that gives some flexibility.
New variable names will be added when they are not needed. Such an artificial separation, when it was only needed to add some notes to string’s content, looks conceptually wrong to me.
Do you think there is no need in inline comments in multiline f-strings?
Why would you want to use comments without an expression? That’s an honest question, I’d expect that the comments are primairily useful to explain complex expressions in the f-string.
BTW. A work around for the need to have an expression is to use one that evaluates to an empty string, such as… an empty string:
f"{'' # some comment
}"
The newline is necessary because Python comments run to the end of a line, using slightly different rules inside f-strings would IMHO lead to additional semantic complexity and introduces more cases where it is no longer possible to copy and expression with a comment into an f-string.
Consider this case, or any other case when one wants to maintain a piece of text/code (SQL, grammar, XML, whatever) embedded in python file. There may be some expressions in this multiline string, as well as complex places where additional comments are needed.
The example above could be broken into 3 separate strings, but what if it is a piece of XML or a highly nested SQL query, where breaking will harm readability? For SQL and XML I could add native inline comments, but for a format not supporting comments the only option would be breaking or using “hacks”. I’m not against hacks in this rare case, but I’d like to have syntax highlighting.
If not introducing completely new syntax for f-string comments, having an ignoring formatting conversion like !i does not seem a complex task (I could even try it myself) and won’t add complexity to Python grammar.
Granted, this won’t be optimized away like some syntax hacks, but I can’t imagine the speed difference being noticeable, especially if this is done only once at import time.
On one hand the expression is complicated enough to warrant a comment.
On the other hand the expression is not complicated enough to warrant a name.
That is quite a combination.
Perhaps if you take the time to come up with a good name, you will not need the comment.
Generally, use the comment capabilities in whatever language you’re embedding in a multi-line string. For example, in SQL
sql = """\
SELECT
name, salary -- the fields I need
FROM
emp -- the employee table
"""
If you aren’t embedding a well-known language, I don’t understand your point about syntax highlighting. But it’s pretty easy to add post-processing to strip lines starting with (say) #:
>>> a = """\
... one
... # Some junk
... two
... """
>>> print("".join(l for l in a.splitlines(keepends=True) if not l.startswith("#")))
one
two
>>>
I don’t think you’ve provided a compelling argument as to why we need an additional comment syntax, especially not if it only works in f-strings and not in all multi-line strings. “It’s possible” isn’t an argument for doing something.