Sorry, this is what happens when I don’t keep a good trace of changes and try to reconstruct code snippets. When I was writing this post, the version of the function in my file was
def preprint(greetings): # factory
def greet_by_name(function): # decorator
@wraps(function)
def wrapper(): # replaces the function being decorated
print(greetings)
return function()
return wrapper
return greet_by_name
In order to reproduce the effect on docstring of bob without using wraps(function), I simply commented that line out, so the function definition became
def preprint(greetings): # factory
def greet_by_name(function): # decorator
# @wraps(function)
def wrapper(): # replaces the function being decorated
print(greetings)
return function()
return wrapper
return greet_by_name
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.
Sorry, this is what happens when I don’t keep a good trace of changes and try to reconstruct code snippets. When I was writing this post, the version of the function in my file was
In order to reproduce the effect on docstring of
bob
without usingwraps(function)
, I simply commented that line out, so the function definition becameAnd
# @wraps(function)
became the docstring...I have fixed this in the post.
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()
.