Python tutorials > Core Python Fundamentals > Basics and Syntax > What are docstrings?
What are docstrings?
'''Docstring'''
or """Docstring"""
) and are placed immediately after the definition of a function, class, module, or method. Docstrings explain what the code does, its parameters, return values, and any exceptions it might raise. They are an essential part of writing readable and maintainable Python code.
Basic Docstring Example
__doc__
attribute.
def greet(name):
'''
This function greets the person passed in as a parameter.
'''
print(f"Hello, {name}!")
Accessing Docstrings
__doc__
attribute of the function, class, or module. The output will be the string you defined within the triple quotes. Accessing docstrings programmatically allows tools like documentation generators (e.g., Sphinx) to automatically create documentation from your code. The example shows an enhanced docstring that includes Args
and Returns
sections. While Python doesn't enforce these sections, they are a common convention.
def greet(name):
'''
This function greets the person passed in as a parameter.
Args:
name (str): The name of the person to greet.
Returns:
None
'''
print(f"Hello, {name}!")
print(greet.__doc__)
Docstrings for Classes
bark
method.
class Dog:
'''
A class representing a dog.
Attributes:
name (str): The name of the dog.
breed (str): The breed of the dog.
'''
def __init__(self, name, breed):
self.name = name
self.breed = breed
def bark(self):
'''
Makes the dog bark.
'''
print("Woof!")
print(Dog.__doc__)
print(Dog.bark.__doc__)
Concepts Behind the Snippet
Real-Life Use Case
Best Practices
Interview Tip
When to Use Them
Basically, document everything that another developer (including your future self) might need to understand to use your code correctly.
Memory Footprint
__doc__
attribute to None
. However, this is rarely necessary.
Alternatives
Docstrings are generally preferred because they are directly integrated with the code and can be accessed programmatically.
Pros
__doc__
attribute.
Cons
FAQ
-
What is the difference between a docstring and a comment?
A docstring is a multiline string literal that appears as the first statement in a module, function, class, or method definition. It is used to document the code and is accessible via the__doc__
attribute. A comment is a single-line or multiline explanation of the code that is ignored by the Python interpreter. Comments are typically used to explain the logic of the code or to provide temporary notes. Docstrings are intended for documentation, while comments are for explanations within the code itself. -
What are some popular docstring styles?
Some popular docstring styles include:- Google Style: Uses sections like Args, Returns, Raises.
- NumPy Style: Similar to Google Style but with more emphasis on documenting numerical code.
- reStructuredText: A more general-purpose markup language that can be used for docstrings.
-
How can I generate documentation from docstrings?
You can use tools like Sphinx to automatically generate documentation from docstrings. Sphinx reads the docstrings in your code and creates HTML or PDF documentation. It supports various docstring styles and can be customized to fit your project's needs.