← Winnitude! Search  ·  Reader Mode (1335 ms)
Links: Stay in Reader Retro Site Converter Retro Mode: OFF Original Page ↗
GITHUB · 25 LINKS

materials/documenting-python-code at master · realpython/materials

Repository navigation

documenting-python-code

Directory actions

More options

Directory actions

More options

Latest commit

 

History


History
View commit history for this file.

documenting-python-code

Folders and files

Name Name Last commit message Last commit date

parent directory

..
README.md README.md    
animal.py animal.py    
comments.py comments.py    
docstring_formats.py docstring_formats.py    
requirements.txt requirements.txt    
say_hello_v1.py say_hello_v1.py    
say_hello_v2.py say_hello_v2.py    
simple_class.py simple_class.py    
spreadsheet_columns.py spreadsheet_columns.py    
type_hints.py type_hints.py    

README.md

Documenting Python Code: A Complete Guide

This folder provides the code examples for the Real Python tutorial Documenting Python Code: A Complete Guide .

Installation

Only the spreadsheet examples need third-party packages. Install them into a virtual environment with:

$ python -m pip install -r requirements.txt

The standalone examples run on Python 3.6 or later. The spreadsheet examples use the pinned pandas version, which requires Python 3.11 or later.

Files

  • comments.py : Functions showing short and multi-line comments.
  • type_hints.py : A function that uses type hints in place of comments.
  • say_hello_v1.py : Sets a function's docstring by assigning to __doc__.
  • say_hello_v2.py : Sets the same docstring with a string literal below the def line.
  • simple_class.py : Shows where class and method docstrings go.
  • animal.py : The Animal class with NumPy/SciPy-style class and method docstrings.
  • spreadsheet_columns.py : A command-line script with a script docstring that prints a spreadsheet's column headers.
  • docstring_formats.py : The spreadsheet function documented in the Google, reStructuredText, NumPy/SciPy, and Epytext formats.

Usage

Inspect docstrings with help():

>>> from say_hello_v2 import say_hello
>>> help(say_hello)
Help on function say_hello in module say_hello_v2:

say_hello(name)
    A simple function that says hello... Richie style

>>> from animal import Animal
>>> Animal("dog", "woof").says()
A dog says woof

Print the column headers of a CSV or Excel spreadsheet:

$ python spreadsheet_columns.py people.xlsx
name
age
city

Run python spreadsheet_columns.py -h to see the script's docstring used as the help message, or run python docstring_formats.py to print the same docstring in all four formats.