• Skip to main content

Annielytics.com

I make data sexy

  • About
  • Tools
  • Blog
  • Tips
  • Portfolio
  • Contact
  • Log In

Sep 25 2025

Docstring Strategy for Python

Python docstrings

If you’re using AI tools as coding assistants, you need to be aware that these tools tend to be lazy coders. They will oftentimes dump a bunch of random variables and methods and generally make your code harder to interpret for those working downstream from your code–or even a later version of yourself working with your code at a later date.

One thing I insist on in my prompt instructions is to wrap all code into functions and include a docstring in those functions. A docstring (short for ‘documentation string’) is a short explanation of what a piece of code does, written directly inside the code itself. Think of it as a how-to manual built right into a function, module, or class. It’s enclosed in triple quotes and is always the first statement.

Python docstring strategy

I like to include three elements:

  • Description of what the function does
  • A list of the arguments
  • What the function returns

An issue I run into regularly though is AI tools will either reduce them to a brief description or remove them altogether, even though I include instructions to never remove comments or docstrings. Whatever style you decide to go with, I highly recommend being consistent, insisting that your AI tool follows your preferred style, and maintaining vigilance against AI’s proclivity toward lazy coding.

Image credit: Jon Tyson

Written by

Copyright © 2025