What is the difference between normal comments (using #) and docstrings (''' ''') in python? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 2

What is the difference between normal comments (using #) and docstrings (''' ''') in python?

24th Jul 2017, 7:51 PM
Ahmed
Ahmed - avatar
4 Answers
+ 1
Everything between the triple quotes is the function’s docstring, which documents what the function does. A docstring, if it exists, must be the first thing defined in a function (that is, on the next line after the function declaration). You don’t technically need to give your function a docstring, but you always should. Python gives you an added incentive: the docstring is available at runtime as an attribute of the function. Many Python IDEs use the docstring to provide context-sensitive documentation, so that when you type a function name, its docstring appears as a tooltip. This can be incredibly helpful, but it’s only as good as the docstrings you write. (from www.diveintopython3.net) (I recommend this online book).
24th Jul 2017, 9:47 PM
yuri
+ 5
Docstrings can be used as multi-line comments, although it's not really their primary purpose. They are used to define multi-line strings of the shape exactly as typed (with spaces, tabs, newlines and other whitespace).
24th Jul 2017, 8:53 PM
Kuba Siekierzyński
Kuba Siekierzyński - avatar
+ 3
doc strings help to provide information for e.g. a class or function def myfunc(): """ This is my function """ if you now call: help(myfunc) it will output: This is my function
1st Aug 2017, 6:24 PM
Martin Ed
Martin Ed - avatar
+ 2
If you use '#', you can write single line comments only. But sometimes when you need to write paragraphs of comments, its not comfortable to write # for every line. So if you use *'''* at the beginning and end, you don't need to use # for every line. As simple as That :)
31st Jul 2017, 3:34 AM
1977leader
1977leader - avatar