GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing www.geeksforgeeks.org — Python Docstrings - GeeksforGeeks Python DocstringsLast Updated : 19 Sep, 2025 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. Declaring and Accessing DocstringsDocstrings 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().
Output 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:
What should a docstring look like?
Types of DocstringsPython supports different docstring styles. Let’s go through them one by one with examples. 1. Triple-Quoted StringsMost 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.
Output 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.
Output This is a docstring using triple double quotes. 2. Google Style DocstringsGoogle 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.
Output 15 Explanation: Args describes input parameters and Returns describes output. 3. Numpydoc Style DocstringsNumpydoc-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.
Output 3.0 4. One-line DocstringsAs 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.
Output Return a raised to power b. 5. Multi-line DocstringsMulti-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.
Output 10 6. Docstrings in Classes (with Indentation)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):
Explanation:
Difference between Comments, String and Docstrings
Example: This example shows the difference between a comment, a string and a docstring.
Explanation:
Comment Explore |
| You are visitor 01402503 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |