John Doe
John Doe

Reputation: 1639

display comments in a python script

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

Upvotes: 1

Views: 228

Answers (1)

rici
rici

Reputation: 241701

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

Upvotes: 1

Related Questions