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?

7th Feb 2025, 9:12 AM
Igor Matić
Igor Matić - avatar
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.
8th Feb 2025, 5:28 AM
Shardis Wolfe
+ 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__)
8th Feb 2025, 8:11 AM
Shardis Wolfe
0
Shardis Wolfe how are docstrings associated with a part of the code? Writing inside a function/class?
8th Feb 2025, 6:21 AM
Igor Matić
Igor Matić - avatar