Python Tutorial

Python Variable

Python Operators

Python Sequence

Python String

Python Flow Control

Python Functions

Python Class and Object

Python Class Members (properties and methods)

Python Exception Handling

Python Modules

Python File Operations (I/O)

Python __doc__ attribute: view documentation

The __doc__ attribute in Python is a special attribute that stores the docstring of a module, class, method, or function. A docstring is a string that serves as documentation for the code object it is associated with. It's written as the first statement in a module, class, method, or function definition and enclosed in triple quotes (either single or double).

In this tutorial, we will learn how to use the __doc__ attribute in Python to access the docstrings of code objects.

  • Defining a docstring for a function:

To define a docstring for a function, simply add a triple-quoted string as the first statement in the function definition.

def greet(name):
    """This function greets the person passed as a parameter."""
    return f"Hello, {name}!"
  • Accessing the docstring of a function:

To access the docstring of a function, you can use the __doc__ attribute of the function object.

print(greet.__doc__)
# Output: This function greets the person passed as a parameter.
  • Defining and accessing docstrings for modules, classes, and methods:

Similarly, you can define and access docstrings for modules, classes, and methods using the __doc__ attribute.

# my_module.py

"""This is the docstring for the module."""

class MyClass:
    """This is the docstring for the class."""

    def my_method(self):
        """This is the docstring for the method."""
        pass
# main.py

import my_module

print(my_module.__doc__)                   # Output: This is the docstring for the module.
print(my_module.MyClass.__doc__)           # Output: This is the docstring for the class.
print(my_module.MyClass.my_method.__doc__) # Output: This is the docstring for the method.
  • Using the help() function:

The help() function in Python displays the docstrings of code objects in a formatted way. You can use this function to get help on modules, classes, methods, and functions.

help(my_module)          # Displays the docstring for the module
help(my_module.MyClass)  # Displays the docstring for the class and its methods

In summary, the __doc__ attribute in Python is used to access the docstrings of code objects, such as modules, classes, methods, and functions. Docstrings serve as documentation for your code and can be accessed using the __doc__ attribute or the help() function.

  1. Documenting functions and classes using doc in Python:

    • Description: In Python, documentation is often added using docstrings. Docstrings are triple-quoted strings at the beginning of a function or class, providing information about their purpose and usage.
    • Code: (example)
      def add_numbers(a, b):
          """
          Adds two numbers.
      
          Parameters:
          - a (int): The first number.
          - b (int): The second number.
      
          Returns:
          int: The sum of a and b.
          """
          return a + b
      
  2. Using doc for interactive help in Python:

    • Description: You can access the docstring of a function or class interactively using the help() function or by using the .__doc__ attribute.
    • Code: (interactive help)
      # Accessing help interactively
      help(add_numbers)
      
      # Accessing docstring programmatically
      print(add_numbers.__doc__)
      
  3. Customizing docstrings for better doc output in Python:

    • Description: Customize docstrings for better documentation output. Tools like Sphinx can generate documentation from docstrings.
    • Code: (advanced example)
      def complex_function(param1, param2):
          """
          Performs complex operations.
      
          :param param1: The first parameter.
          :type param1: int
      
          :param param2: The second parameter.
          :type param2: str
      
          :return: The result of the complex operation.
          :rtype: float
      
          :raises ValueError: If param2 is not a valid string.
      
          .. note::
              Additional notes about the function.
          """
          # Function implementation