Is there a reason to write a comment at the beginning inside the function?


#1

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.


FAQ: Learn Python: Functions - Function Junction
#2

It purely depends on you whether you want to write a comment at the beginning of a function. But adding a comment to your code make it much readable and simpler for you to understand in future reference . A code with a comment give a good impression of your coding Style . If your code lacks a comment it would be very difficult for another person to understand the code and gives an impression of a messy style . It will also help you a lot when you are writing a long code such as back end framework of a website where we write 1000s lines of code