CKTIS / Python
Comments and documentation
A # outside a string literal begins a comment that continues to the end of the physical line.
Version: Python 3.14; repository image, production runtime not attested
Syntax
# comment
def label():
"""Describe the result."""
return '#tag'A # outside a string literal begins a comment that continues to the end of the physical line.
Ordinary comments are ignored. A leading string literal in a function body can supply its docstring through __doc__. A hash in literal text stays data. Since 3.12 a # in the expression of an f-string replacement field can start a comment through the end of its line.
The function returns #tag and exposes Return a literal tag. as its documentation.
import ast
tree=ast.parse('def f():\n """About f."""\n # explanation\n return "#"')
assert ast.get_docstring(tree.body[0])=='About f.'
assert len(tree.body[0].body)==2
assert ast.literal_eval(tree.body[0].body[-1].value)=='#'
text='f"""{1 # comment\n+2}"""'
assert eval(text)=='3'
assert '#tag'=='#'+'tag'
Examples
def label():
"""Return a literal tag."""
# A comment cannot change the returned string.
return "#tag"
print(label(), label.__doc__)
Result
#tag Return a literal tag.
The function returns #tag and exposes Return a literal tag. as its documentation.