Question
Is there a reason to write a comment at the beginning inside the function?
Answer
Yes! Not only does it help you and other people who read your code understand what’s going on, but it also serves a more practical purpose: documentation. If you were to publish a library for others to be able to use, like the datetime
library we used earlier, and someone wanted to know how to use a method belonging to your library, they could type help(function_name)
into their Python interpreter and, if your function has this standard documentation, it will be displayed to them to help them understand how to use your awesome code!
For an in-depth look at this standard, take a look at the PEP docstring style guide.