1

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

4
  • I think only using docstrings Commented Dec 27, 2019 at 16:14
  • docstrings are ignored if not just after the function definition, aren't they ? Commented 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. Commented Dec 27, 2019 at 16:26
  • @rici thanks, I will accept as an answer if you do. Commented Dec 27, 2019 at 17:17

1 Answer 1

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.)

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.