CKTIS / Python
Commenti e documentazione
Un # fuori da un letterale stringa apre un commento fino alla fine della riga fisica.
Versione: Python 3.14; repository image, production runtime not attested
Sintassi
# comment
def label():
"""Describe the result."""
return '#tag'Un # fuori da un letterale stringa apre un commento fino alla fine della riga fisica.
Il parser ignora i commenti ordinari. Un letterale stringa iniziale nel corpo di una funzione può fornire la documentazione accessibile con __doc__. Nel testo letterale # è un dato. Da 3.12, nell’espressione di un campo di f-string, # può iniziare un commento fino a fine riga.
La funzione restituisce #tag e la sua documentazione è Return a literal tag.
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'
Esempi
def label():
"""Return a literal tag."""
# A comment cannot change the returned string.
return "#tag"
print(label(), label.__doc__)
Risultato
#tag Return a literal tag.
La funzione restituisce #tag e la sua documentazione è Return a literal tag.