Python – Docstring – Help and Comments

Docstrings in Python are actually quite simple – the are 3 times this """ and the code between opening and closing them can be formatted in any possible way.

The reason I have decided to dedicate a whole article to them (other that I probably do not have anything better to do) is that they can be called through the help() function of python.

The beautiful Bulgarian city of Botevgrad has nothing to do with docstrings.

Let’s see how it works. Imagine the following file:

If we navigate to the folder of the file and open python, then we may use the magic words import vitoshacademy. As we have the words print("Thank you for using VitoshAcademy.com!") on the top of the module, we will see the following:

Which actually can make you smile a bit. Furthermore, once imported, we may start using help():

To return back to Python from vim, press :q or google “how to exit vim”. If you want to display the help of specific function, then its class should be imported (if applicable) and is should be referred:

If you want to refer to the comment in a function, then the __doc__ is your friend, so that the following:

returns:

So, that should be it. Thanks for reading 🙂

Tagged with: , , , , ,