Python DocString and Function Annotation

Docstring stands for documentation strings. Python docstrings are the string literals that appear right after the definition of a function, method,  class or module. It gives an idea to developer for what purpose the function is written for.

Reasons to write Docstrings:

  1. It makes it easier for other people to use the function effectively.
  2. Writing documentation before writing code for your function will give a clear idea what function will do.

Note: While in C++ and Java, docstrings are written before function declaration, in python they are written inside function definition as everything in python is object so docstrings acts like an attribute to function.

  • Declaring Docstrings: The docstrings are declared using ”’triple single quotes”’ or “”” triple double quotes “”” just below the class, method, or function declaration. All functions should have a docstring.
  • Accessing Docstrings: The docstrings can be accessed using the __doc__ method of the object or using the help function. The below examples demonstrate how to declare and access a docstring.

Example:

def add_binary(a, b):
    '''
    Return the sum of two decimal numbers in binary digits.

           
              :param a : A decimal integer
              :param b : Another decimal integer

              : return : Binary string of the sum of a and b
    '''
    binary_sum = bin(a+b)[2:]
    return binary_sum


print(add_binary.__doc__)
print(add_binary())

Here in the above example inside the triple quotes docstring first we will specify the function definition what it will do then the parameters passed and end it with return parameter i.e what it is going to return . Also the add_binary.__doc__ is used to get the docstring we wrote for the add_binary() function. Even if we hover our mouse to the calling of function i.e at add_binary() we would be able to see the docstring.

What is a Module ?

In layman terms module is basically a file containing python definition and statement i.e function, class definition and other python statements. So all the file we save with .py extension it actually becomes a new python module which we can import in another .py file if needed.

What is the difference between Comment and Docstring ?

  • Comments: Comments in Python start with a hash mark (#) and are intended to explain the code to developers. They are ignored by the Python interpreter.
  • Docstrings: Docstrings provide a description of the function, method, class, or module. Unlike comments, they are not ignored by the interpreter and can be accessed at runtime using the .__doc__ attribute.

Function Annotation :

Annotating a function means while defining the function we would specify the type of parameter to pass and also the return type of value that function is going to return. They are mainly used to improve code readability and provide hints to developer. They are arbitrary python expressions that are associated with various part of functions. These expressions are evaluated at compile time and have no life in python’s runtime environment.

The syntax for function annotations is straightforward:

def function_name(parameter: annotation) -> return_annotation:
    # function body
  • Parameter annotations: Placed after the parameter name, separated by a colon.
  • Return annotations: Placed after the parameter list, separated by an arrow (->).

Example:

def is_pallindrome(string:str)->bool:

“””

Check if a string is a pallindrome. A pallindrome is a string that reads same forwards and backwards.

: param string : String to check

: return : True if a string is a pallindrome false otherwise

return string[ : : -1].casefold()==string.casefold()

p= is_pallindrome()

Here in the above example function annotation is :

  1. string : str – It determines that the value passed to string will be a str type
  2. -> bool – The function is going to return bool type value (True or False)

Leave a Reply

Your email address will not be published. Required fields are marked *