Is there a way, beside parsing the file, to display the comments in a Python file ? As in :
d = {
# key value uses
k = v
}
I would display :
# key value uses
in the function __doc__.
Thanks
asked Dec 27, 2019 at 16:11
John Doe
1,6593 gold badges14 silver badges25 bronze badges
-
I think only using docstringsLucas Wieloch– Lucas Wieloch2019年12月27日 16:14:21 +00:00Commented Dec 27, 2019 at 16:14
-
docstrings are ignored if not just after the function definition, aren't they ?John Doe– John Doe2019年12月27日 16:15:41 +00:00Commented Dec 27, 2019 at 16:15
-
@JohnDoe: yes, and comments are always deleted. So you'll have to parse the source yourself if you want them.rici– rici2019年12月27日 16:26:01 +00:00Commented Dec 27, 2019 at 16:26
-
@rici thanks, I will accept as an answer if you do.John Doe– John Doe2019年12月27日 17:17:49 +00:00Commented Dec 27, 2019 at 17:17
1 Answer 1
Python always deletes (and docstrings not at the beginning of a definition). So you'll have to parse the source yourself if you want to extract them.
The standard library's ast module also drops comments, but you could take a look at the tokenize module, which returns them. (However, it doesn't parse, so you'd still need to do some work to associate the comment with its function or class or whatever.)
answered Dec 27, 2019 at 18:16
rici
243k30 gold badges263 silver badges364 bronze badges
Sign up to request clarification or add additional context in comments.
Comments
lang-py