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)
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.
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}!"
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.
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.
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.
Documenting functions and classes using doc in Python:
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
Using doc for interactive help in Python:
help()
function or by using the .__doc__
attribute.# Accessing help interactively help(add_numbers) # Accessing docstring programmatically print(add_numbers.__doc__)
Customizing docstrings for better doc output in Python:
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