0
Help understanding a note.
"Unlike conventional comments, docstrings are retained throughout the runtime of the program. This allows the programmer to inspect these comments at run time." What is meant by "runtime of a program" and why can't the same thing be done with comments?
3 Answers
+ 1
Igor MatiÄ Comments intended to clarify/explain brief sections of code, or used by programmers for notes about unfinished code.
Docstrings are intended to provide documentation for larger code structures like entire function or a class. Stuff like what the function does, what arguments are expected and any limits on those arguments, and what (if any) result is returned.
Docstrings are accessible at runtime using __doc__ attribute of the associated structure.
Like some_function.__doc__
Or some_class.__doc__
Regular comments don't have this attribute association, which is why you can only view them by looking at the uncompiled py file.
+ 1
Igor MatiÄ yes. They are first thing inside block they are documenting. Using 3 quotes (single or double) to mark start and end.
EXAMPLE
def some_function( ):
'''
This is docstring for
the function some_function.
'''
print("Random Stuff")
print(some_function.__doc__)
0
Shardis Wolfe how are docstrings associated with a part of the code? Writing inside a function/class?