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

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.

3 Likes

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

7 Likes

Is there a reason why we don’t use # here?

12 Likes

The Python interpreter gives special status to a comment that is between triple quotes and that is the first item after def in a function definition.

If you are running a module or script whose functions contain those comments - called docstrings - then at your console, you can call

help(function_name)

… and the docstring will be printed to the screen.

(You can also incorporate some (primitive) testing capability in the docstring.)

(Comment, two hours later: I just noticed that box-in-box thing. It is a by-product of trying to type and edit the comment on my phone; I have no idea how I did it. Pretty cool, though!)

9 Likes

The box-in-box-thing is markdown interpreting the python prompt. >>>

>>> def foo(x):
    '''
    a function to return x
    '''
    return x

>>> print (foo.__doc__)

    a function to return x

>>> 
4 Likes

Ah, and I just thought it was due to clumsy fingers!

2 Likes

Wow I’ve never heard of docstrings before. That’s very helpful to learn. Many thanks!

2 Likes

Is it compulsory to add a comment when defining a function?

1 Like

Not compulsory, no, although it does help to give a function a docstring that can be read with the help() builtin function.

def add(a, b):
    """
    a function that takes two inputs and returns their sum
    """
    return a + b
>>> help(add)
a function that takes two inputs and returns their sum
>>> 
3 Likes

Hello. im having trouble.

take a look at thisa a see what has happend.

Define your spam function starting on line 5. You

can leave the code on line 10 alone for now–we’ll

explain it soon!

def spam():

“”“Prints ‘Eggs!’”""

print “Eggs!”

Define the spam function above this line.

spam()

Its not working!

Please see the following- How do I format code in my posts? and format your code as it’s not clear how your function is set up at the moment. If this is for a specific lessons then please link it too.

is confusing to me because i still think that def is a like a variable.
is def an already built in function like print or what?

def is a standard keyword, short for function definition. It is not a variable, which refers to an object; a function is an object.