![]() |
VOOZH | about |
Docstrings (Documentation Strings) are special strings used to document Python code. They provide a description of what a module, class, function or method does.
Example:
Here, the docstring explains what the function does in one simple line.
Docstrings are written just below the function, class or module definition. They can be accessed later using either:
Example: The following code shows how to declare a docstring for a function and access it using both __doc__ and help().
Using __doc__: Demonstrates triple quotes docstring and does nothing. Using help(): Help on function my_function in module __main__: my_function() Demonstrates triple quotes docstring and does no...
Explanation:
Python supports different docstring styles. Let’s go through them one by one with examples.
Most common style in Python. Uses triple single (''') or triple double (""") quotes. Can span multiple lines.
Example 1: This function shows how to use triple single quotes for docstrings.
Using __doc__: This is a docstring using triple single quotes. Using help(): Help on function my_func in module __main__: my_func() This is a docstring using triple single quotes.
Explanation: docstring is written just below the function definition. __doc__ prints it directly and help() shows it in a structured way.
Example 2: This function shows how to use triple double quotes for docstrings.
This is a docstring using triple double quotes.
Google style docstrings follow a specific format and are inspired by Google's documentation style guide. They provide a structured way to document Python code, including parameters, return values and descriptions.
Example: This function multiplies two numbers using Google-style docstrings.
15
Explanation: Args describes input parameters and Returns describes output.
Numpydoc-style docstrings are widely used particularly for documenting functions and classes related to numerical computations and data manipulation. It is an extension of Google-style docstrings, with some additional conventions for documenting parameters and return values.
Example: This function divides two numbers using Numpydoc-style docstrings.
3.0
As the name suggests, one-line docstrings fit in one line. They are used in obvious cases. The closing quotes are on same line as the opening quotes. This looks better for one-liners.
Example: This function raises a number to a power with a one-line docstring.
Return a raised to power b.
Multi-line docstrings consist of a summary line just like a one-line docstring, followed by a blank line. The summary line may be on same line as opening quotes or on next line.
Example: This function shows a multi-line docstring with parameters and return value.
10
Docstrings in classes describe the class itself, its attributes, and its methods. Proper indentation is important:
Example: This example shows class-level and method-level docstrings with proper indentation.
Output (using help):
Help on class Employee in module __main__:
class Employee(builtins.object)
| A class representing an employee.
|
| Attributes:
| name (str): Employee name.
| age (int): Employee age.
| salary (float): Employee salary.
|
| Methods defined here:
|
| __init__(self, name, age, salary)
| Initialize an Employee object.
|
| promote(self, amount)
| Increase salary by a given amount.
Explanation:
Note: Docstrings are actually strings too, but Python treats them specially when placed right after a function, class or module definition.
Example: This example shows the difference between a comment, a string and a docstring.
Explanation: