Are 'Docstrings' same as 'Comments' in Python? Or do they have any special purpose? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 2

Are 'Docstrings' same as 'Comments' in Python? Or do they have any special purpose?

As far as I understand docstrings are somewhat similar to multi line comments used in other programming languages, but this is the part which I don't understand: "Unlike conventional comments, docstrings are retained throughout the runtime of the program. This allows the programmer to inspect these comments at run time."

7th Mar 2018, 9:41 AM
Rusty.Metal
3 Answers
+ 4
What I got from google: "AĀ docstringĀ is a string literal that occurs as the first statement in a module, function, class, or method definition. Such aĀ docstringĀ becomes the __doc__ special attribute of that object. All modules should normally haveĀ docstrings, and all functions and classes exported by a module should also haveĀ docstrings."
7th Mar 2018, 9:44 AM
luŹi
+ 1
You can use it as comments or that part of your coding related works which will not be a part of code but it will work as explanations or comments.
7th Mar 2018, 12:18 PM
šŸ“ˆSmileGoodHopešŸ“ˆ
šŸ“ˆSmileGoodHopešŸ“ˆ - avatar
0
the piece of doc string will show up in the ide when you type the associated class/method. the comment won't
7th Mar 2018, 10:52 AM
seamiki
seamiki - avatar