AI Generate docstrings automatically

Python docstring style guide

Learn Python docstring conventions and best practices. Write clear, consistent function and class documentation following industry standards.

Documentation structure

Essential docstring components for Python

Python documentation follows Triple-quoted strings with various formats (Google, NumPy, Sphinx). Common tools like Sphinx and pydoc help generate and maintain Python documentation.

Python -specific documentation features

  • Docstrings are accessible via __doc__ attribute
  • Multiple accepted formats: Google, NumPy, reStructuredText
  • Type hints work alongside docstrings

1.Summary description

Start with a clear, concise one-line summary of what the function or class does. Use present tense and action verbs. Follow with a more detailed explanation if needed.

/**

* Calculate the total price including tax and shipping.

*

* This function applies the appropriate tax rate based on

* the customer's location and adds shipping costs.

*/

2.Parameters

Document each parameter with its name, type, and purpose. Include whether it's optional and any default values. Explain valid ranges or acceptable values.

* @param {number} subtotal - The pre-tax total amount

* @param {string} location - Customer's country code (ISO 3166)

* @param {Object} options - Optional configuration

* @param {boolean} [options.express=false] - Use express shipping

3.Return values

Specify what the function returns, including the type and a description of the value. For complex return types, document the structure.

* @returns {Object} Pricing breakdown

* @returns {number} returns.total - Final total with tax and shipping

* @returns {number} returns.tax - Tax amount applied

* @returns {number} returns.shipping - Shipping cost

4.Exceptions and errors

Document any exceptions or errors that might be thrown, including when and why they occur. This helps users handle errors properly.

* @throws {ValidationError} If subtotal is negative

* @throws {LocationError} If location code is invalid

* @throws {ConfigError} If required tax config is missing

5.Usage examples

Provide clear examples showing how to use the function or class. Include common use cases and edge cases when helpful.

* @example

* // Calculate total for US customer

* const result = calculateTotal(99.99, 'US');

* console.log(result.total); // 107.99

*

* @example

* // With express shipping

* const result = calculateTotal(99.99, 'US', \{ express: true \});

6.Additional metadata

Include other relevant information like deprecation notices, version history, related functions, or links to external resources.

* @since 2.0.0

* @see \{@link applyDiscount\} for discount calculations

* @deprecated Use calculateTotalV2() instead

* @author John Doe

Generate professional docstrings for your Python code with DocuWriter.ai

View examples

Want to learn more? Check out our guide on technical documentation best practices

FAQ

Frequently asked questions

What is a docstring?

A docstring is a string literal that appears as the first statement in a module, function, class, or method to document what it does and how to use it.

What should I include in docstrings?

Include a brief description, parameters with types, return values, exceptions raised, and usage examples when helpful.

Are there standard docstring formats?

Yes, common formats include JSDoc for JavaScript, PHPDoc for PHP, Javadoc for Java, and various styles for Python (Google, NumPy, reStructuredText).

Related resources

Get started

Start documenting and organizing your codebase