On 4/4/2022 11:07 AM, Coyot Linden (Glenn Glazer) wrote:
I would like to point out another use case of triple quotes outside of
docstrings.
A docstring is *any* string literal, regardless of quotes, that is a
statement in itself and is the *first* statement in the body of a
module, class, or function. Triple quotes are routinely used for
multiline strings elsewhere.
We do a lot of SQL here and so doing a parameterized query
like:
"""SELECT foo
FROM bar
WHERE baz = %s"""
Others do the same. To be useful, you have to either bind the query
string to something for use later
query = """query string"""
or immediately use it in an expression
cursor = """SELECT %s"" % 'abc'
In neither case will it be interpreted as a docstring.
I would welcome a multiline comment format that didn't involve docstrings.
Some people already use multiline strings for that, in positions where
they do not become the docstring. At least in CPython, comment strings,
at least in functions, appear to be omitted from the code object.
def f():
'docstring'
'other string'
fc = f.__code__
fc.co_consts
# ('docstring', None) # 'other string' absent.
import dis
dis.dis(f) # 'other string not loaded from anywhere else.
# 1 0 RESUME 0
#
# 3 2 LOAD_CONST 1 (None)
# 4 RETURN_VALUE
--
Terry Jan Reedy
_______________________________________________
Python-Dev mailing list -- [email protected]
To unsubscribe send an email to [email protected]
https://mail.python.org/mailman3/lists/python-dev.python.org/
Message archived at
https://mail.python.org/archives/list/[email protected]/message/QBRWBJW6NTBD4LIFENUX7U6EUA5BISF7/
Code of Conduct: http://python.org/psf/codeofconduct/