Looks like help() will fall back to reading a comment above a definition if it can’t find a docstring, but only if it’s in an accessible source file. Functions do know their line numbers for debugging purposes. If you try this with a definition in the REPL, or in an exec’d string or .pyc file (without corresponding source), this won’t work, because there is no file to read the comments from, unlike a docstring which would be available at runtime when read from any of these sources.
Also, if you modify the source file after importing the object, then the line numbers won’t match up and it might find the wrong comment (when it reads the modified source file to look for comments). This can’t happen with docstrings either, since they’re saved when the object is created.
Looks like
help()
will fall back to reading a comment above a definition if it can’t find a docstring, but only if it’s in an accessible source file. Functions do know their line numbers for debugging purposes. If you try this with a definition in the REPL, or in anexec
’d string or.pyc
file (without corresponding source), this won’t work, because there is no file to read the comments from, unlike a docstring which would be available at runtime when read from any of these sources.Also, if you modify the source file after importing the object, then the line numbers won’t match up and it might find the wrong comment (when it reads the modified source file to look for comments). This can’t happen with docstrings either, since they’re saved when the object is created.
See also
inspect.getcomments()
.